springboot设置虚拟目录,通过url访问上传的图片

关于这个springboot虚拟目录的设置,网上虽然也有很多的相关的博客教程,但是自己还是写博客记录下来吧!
1.首先在application.yml文件设置上传路径和静态资源访问路经

#application.yml格式
uploadFile:
  path: C:\upload\   #(注意了后面一定加“\”)
  staticAccessPath: /upload/**

#application.properties格式
uploadFile.path=C:\upload\
uploadFile.staticAccessPath=/upload/**

2.写一个配置类,进行虚拟目录的配置

@Configuration
public class UploadFilePathConfig implements WebMvcConfigurer {

    @Value("${uploadFile.path}")
    private String uploadFilePath;

    @Value("${uploadFile.staticAccessPath}")
    private String staticAccessPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+uploadFilePath);
    }
}

3.参考
https://blog.csdn.net/superlover_/article/details/80893007
https://www.cnblogs.com/zuidongfeng/p/8859235.html

发布了14 篇原创文章 · 获赞 6 · 访问量 6329

猜你喜欢

转载自blog.csdn.net/weixin_43817709/article/details/100859132