springBoot打成jar包后上传的静态资源如何存储和访问
springboot项目打成jar包后如何设置文件资源存储路径1.设置文件存储路径为jar包的同级//保存上传的资源文件路径,路径在部署jar包同级目录。String path = System.getProperty("user.dir")+"/static/images/upload/";File dir = new File(path);// 如果不存在则创建目录if(!dir.exists
·
springboot项目打成jar包后如何设置文件资源存储路径
1.设置文件存储路径为jar包的同级
//保存上传的资源文件路径,路径在部署jar包同级目录。
String path = System.getProperty("user.dir")+"/static/images/upload/";
File dir = new File(path);
// 如果不存在则创建目录
if(!dir.exists()){
dir.mkdirs();
}
此时上传的文件再springboot jar包的同级目录 static/images/upload
文件保存到该目录后如何通过http访问该文件呢?
2.设置http访问上传的静态资源文件
package com.example.config;
import com.example.interceptor.SecureInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 拦截器配置
*
* @author Xu
* @date 2022/4/1 15:03
**/
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/","file:static/");
}
}
接下来就可以通过http://ip:port/images/upload/xxx来访问该文件了
更多推荐
已为社区贡献4条内容
所有评论(0)