addResourceHandlers(ResourceHandlerRegistry registry)中文无法访问

一 问题复现

使用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

版本问题,最为致命


addResourceHandlers(ResourceHandlerRegistry registry)中文无法访问
https://yangxj96.github.io/2022/12/java/b006/
作者
yangxj96
发布于
2022年12月22日
许可协议