springboot virtual directory, access to upload images via url

About this setting springboot virtual directory, although there are a lot of online-related blog tutorials, but it is still writing blog to record it!
1. First set the upload path and static resource access path file in application.yml

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

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

2. Write a class configuration, virtual directory configuration

@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. Reference
https://blog.csdn.net/superlover_/article/details/80893007
https://www.cnblogs.com/zuidongfeng/p/8859235.html

Published 14 original articles · won praise 6 · views 6329

Guess you like

Origin blog.csdn.net/weixin_43817709/article/details/100859132