一 问题复现
使用springboot配置映射静态资源目录后,英文和数字可以正常进行访问,但是中文就提示404
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/file/video/**").addResourceLocations(props.getVideoFolder());
}
二 解决办法
原文传送门,有分析过程
一 UrlPathHelper 设置不decodeurl
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
urlPathHelper.setDefaultEncoding(StandardCharsets.UTF_8.name());
configurer.setUrlPathHelper(urlPathHelper);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/files/**").addResourceLocations("file:E:/FileUpload/HmiInterface/");
}
}
二 使用原来的AntPathMatcher (推荐)
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
版本问题,最为致命

本文档主要介绍了在SpringBoot中配置静态资源目录时遇到的中文路径404错误。问题源于URL解码设置,通过调整UrlPathHelper的配置,设置为不decodeURL并指定默认编码为UTF-8,解决了中文路径无法访问的问题。同时提供了使用AntPathMatcher作为匹配策略的备选解决方案。注意,此问题可能与SpringBoot版本有关。
1424

被折叠的 条评论
为什么被折叠?



