springboot 項目打包部署後設置上傳文件訪問的絕對路徑

一、設置絕對路徑

application.properties的配置markdown

#靜態資源對外暴露的訪問路徑 file.staticAccessPath=/upload/** #文件上傳目錄(注意Linux和Windows上的目錄結構不一樣) #file.uploadFolder=/home/upload/ file.uploadFolder=d://upload/

配置上傳文件的目錄,也能夠在這裏設置上傳文件的大小app

@Configuration public class UploadFileConfig { @Value("${file.uploadFolder}") private String uploadFolder; @Bean MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setLocation(uploadFolder); //文件最大
        factory.setMaxFileSize("5MB"); // 設置總上傳數據總大小
        factory.setMaxRequestSize("10MB"); return factory.createMultipartConfig(); } }

二、設置虛擬路徑,訪問絕對路徑下資源ide

@Configuration public class UploadFilePathConfig extends WebMvcConfigurerAdapter { @Value("${file.staticAccessPath}") private String staticAccessPath; @Value("${file.uploadFolder}") private String uploadFolder; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder); } }

 

這時就能夠經過訪問 http://localhost:8080/upload/xxx.jpg訪問d://upload/下的資源。spa

相關文章
相關標籤/搜索