Springboot上传到本地并显示

配置方式实现

1. 配置方式就是实现WebMvcConfigurer, 将配置文件中文件路径也当做项目的的静态资源路径,同内置的static、public约定文件夹一样,配置注意一下两个事项

项目中的资源是使用classpath:/开头,而本地文件夹(windows下)需要使用file:/ 开头,使用文件协议加载文件
结尾一定要有/或者\,推荐使用/,如果不加/访问文件不显示。


@Configuration
public class WebAppConfigurerConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //结尾一定要有/或者\,推荐使用/
        //linux和mac下面不用添加file开头
        registry.addResourceHandler("/images/**").addResourceLocations("/Users/admin/logs/images/");
        //mac下面添加file也会显示
        //registry.addResourceHandler("/images/**").addResourceLocations("file:/Users/admin/logs/images/");
        //windows
       // registry.addResourceHandler("/images/**").addResourceLocations("file:" + "C:\Users\admin\logs\images\");


    }
}

  


访问路径
http://localhost:8010/images/952064dfc5b7466e80654332298ee78f.JPG
 


原文地址:https://www.cnblogs.com/cdchencw/p/13048324.html