[Turn] springboot upload the configuration file and can access the uploaded path

1. application.yml profile adds

# Upload files related 
File: 
  # file upload directory 
  #uploadFolder: / usr / local / the Upload 
  uploadFolder: D: // upfile / 
  # resource path of external access 
  staticAccessPath: / upfile / **

2. Configure upload limit

@Configuration
public class UploadFileConfig {

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation(uploadFolder);
        //文件最大
        factory.setMaxFileSize("10MB");
        // 设置总上传数据总大小
        factory.setMaxRequestSize("10MB");
        return factory.createMultipartConfig();
    }

}

3. Configure virtual path

@Configuration
 public  class FileMvcConfig the implements WebMvcConfigurer { 

    @Value ( "file.staticAccessPath $ {}" )
     Private String staticAccessPath; 

    @Value ( "file.uploadFolder $ {}" )
     Private String uploadFolder; 

    @Override 
    public  void addResourceHandlers (ResourceHandlerRegistry Registry) {
         // file disk Image url mapping
         // configuration server virtual path, handler directory access to the front desk, locations for the files corresponding to the local path 

        registry.addResourceHandler (staticAccessPath) .addResourceLocations ( "file:" + uploadFolder); 
    } 
}

Browser to access http: //localhost/upfile/1.txt

You can get to D: // upfile / file 1.txt below

Guess you like

Origin www.cnblogs.com/lschuan/p/11413954.html