springboot通过重写addResourceHandlers拦截请求访问本地资源

通过继承WebMvcConfigurer重写addResourceHandlers 例如当请求为
localhost:8080/photo/** 就会去访问本地资源的内容

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
 @Value("${server.resource}")
 /**
     * 配置资源访问
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 静态资源---文件url地址
        registry.addResourceHandler("/photo/**").addResourceLocations("file:"+ ConstansPropertity.filePath);
        // 静态资源---视频url地址
        registry.addResourceHandler("/video/**").addResourceLocations("file:"+ConstansPropertity.videoPath);
         // 上传视频缩略图url地址
        registry.addResourceHandler("/photo/**").addResourceLocations("file:"+ConstansPropertity.videoIcon);
         //上传照片缩略图url地址
        registry.addResourceHandler("/img/**").addResourceLocations("file:"+ filePath+"Camera/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }   private  String filePath;
}
发布了78 篇原创文章 · 获赞 5 · 访问量 7359

猜你喜欢

转载自blog.csdn.net/weixin_41930050/article/details/104538132