1,对外暴露静态资源路径
#对外暴露的静态资源路径
file. upload.static-access-path= /uploads/**
#本地磁盘路径
file. upload.folder= E:/uploads/
//静态资源对外暴露的访问路径
@Value("${file.upload.static-access-path}")
private String staticAccessPath;
@Value("${file.upload.folder}")
private String deskFolder;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//将本地磁盘的指定路径映射成Spring Boot的静态资源路径
registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+deskFolder);
}
这样前端可以通过"/uploads/"前缀进行访问。
2,匹配中文文件名
遇到文件名为中文时,需要进行如下配置:
方法1,配置 WebMvcConfigurer
引入configurePathMatch配置项,直接设置不decodeurl
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
//设置不decodeurl
urlPathHelper.setUrlDecode(false);
urlPathHelper.setDefaultEncoding(StandardCharsets.UTF_8.name());
configurer.setUrlPathHelper(urlPathHelper);
}
方法2,直接在application配置文件中将策略指定为ant-path-matcher
spring.mvc.pathmatch.matching-strategy= ant_path_matcher