spring boot 2.x文件路径映射问题汇总

当我们在运行可执行的java jar包的时候,我们肯定改变不了jar里面的内容,因此文件上传路径就成了我们必须考虑的一点问题,我们不能往直接这个jar包里面写文件,那么只能写在jar包外面,但是写到jar包外面的时候要怎么通过内置的tomcat下载文件呢?springboot给我们提供了一种方式,可以将jar包外部路径映射到jar里面。接下来我们看看如何实现。
1、springboot2.0以前:我们通过继承WebMvcConfigurerAdapter来实现文件路径的映射。

@Configuration
public class FileConfig extends WebMvcConfigurerAdapter {

    private String uploadPath = E://file/;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/file/**").addResourceLocations("file:" uploadPath);
        super.addResourceHandlers(registry);
    }
}

2、WebMvcConfigurerAdapter的内部实现WebMvcConfigurerAdapter实现了WebMvcConfigurer接口的所有空方法。

@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
    public WebMvcConfigurerAdapter() {}
        
    public void configurePathMatch(PathMatchConfigurer configurer) {}
        
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {}

    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {}

    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {}

    public void addFormatters(FormatterRegistry registry) {}

    public void addInterceptors(InterceptorRegistry registry) {}

    public void addResourceHandlers(ResourceHandlerRegistry registry) {}

    public void addCorsMappings(CorsRegistry registry) {}

    public void addViewControllers(ViewControllerRegistry registry) {}

    public void configureViewResolvers(ViewResolverRegistry registry) {}

    public void addArgumentResolvers(List argumentResolvers) {}

    public void addReturnValueHandlers(List returnValueHandlers) {}

    public void configureMessageConverters(List> converters) {}

    public void extendMessageConverters(List> converters) {}

    public void configureHandlerExceptionResolvers(List exceptionResolvers) {}

    public void extendHandlerExceptionResolvers(List exceptionResolvers) {}
    @Nullable
    public Validator getValidator() {
        return null;
    }

    @Nullable
    public MessageCodesResolver getMessageCodesResolver() {
        return null;
    }
}

3、WebMvcConfigurerAdapter为什么会过时?这是因为由于Java的版本更新,在Java 8中,可以使用default关键词为接口添加默认的方法,Spring在升级的过程中也同步支持了Java 8中这一新特性。
4、新选择WebMvcConfigurer我们看一下它实现的接口WebMvcConfigurer,既然WebMvcConfigurer默认有了空实现,我们直接实现WebMvcConfigurer接口即可。

public interface WebMvcConfigurer {
    default void configurePathMatch(PathMatchConfigurer configurer) {}

    default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {}

    default void configureAsyncSupport(AsyncSupportConfigurer configurer) {}

    default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {}

    default void addFormatters(FormatterRegistry registry) {}

    default void addInterceptors(InterceptorRegistry registry) {}

    default void addResourceHandlers(ResourceHandlerRegistry registry) {}

    default void addCorsMappings(CorsRegistry registry) {}

    default void addViewControllers(ViewControllerRegistry registry) {}

    default void configureViewResolvers(ViewResolverRegistry registry) {}

    default void addArgumentResolvers(List resolvers) {}

    default void addReturnValueHandlers(List handlers) {}

    default void configureMessageConverters(List> converters) {}

    default void extendMessageConverters(List> converters) {}

    default void configureHandlerExceptionResolvers(List resolvers) {}

    default void extendHandlerExceptionResolvers(List resolvers) {}

    @Nullable
    default Validator getValidator() {
        return null;
    }

    @Nullable
    default MessageCodesResolver getMessageCodesResolver() {
        return null;
    }
}

5、更新后的代码如下:

@Configuration
public class FileConfig implements WebMvcConfigurer {

    private String uploadPath = E://file/;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/file/**").addResourceLocations("file:" uploadPath);
        super.addResourceHandlers(registry);
    }
}

如果各位想直接使用,且用的spring boot版本在2以上,则复制第五点到项目中就好了。最后祝广大程序猿1024快乐。多长头发,少些bug。

如果文章对您有帮助,请记得点赞关注哟~

欢迎大家关注我的公众号 ,每日推送技术文章供大家学习参考。

你可能感兴趣的:(springboot,springboot,文件路径)