Spring boot實現上傳文件到本地服務器
本文實例為大傢分享瞭Spring boot實現上傳文件到本地服務器的具體代碼,供大傢參考,具體內容如下
引入配置
UploadFileConfig類
@Configuration public class UploadFileConfig { @Value("${file.uploadFolder}") private String uploadFolder; @Bean MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setLocation(uploadFolder); //文件最大 factory.setMaxFileSize("20MB"); return factory.createMultipartConfig(); } }
UploadFilePathConfig類
@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); } }
在application.properties加上傳的配置項
#文件服務本地rest請求對外地址 file.staticAccessPath=/upload/** #文件上傳目錄(註意Linux和Windows上目錄結構不同)需要改成你自己的實際目錄 file.uploadFolder=/Users/leichunhong/Documents/hlp/zx/ #上傳文件本地域名 改寫成你後端服務的地址和端口 file.upload.nama=http://127.0.0.1:9090 #springBOOt上傳文件大小 # 單個文件的最大值 spring.servlet.multipart.max-file-size = 10MB # 上傳文件總的最大值 spring.servlet.multipart.max-request-size=100MB
編寫上傳controller
@RestController @RequestMapping("/test") public class UploadController { @Value("${file.uploadFolder}") private String uploadFolder; @Value("${file.staticAccessPath}") private String staticAccessPath; @Value("${file.upload.nama}") private String reurl; @RequestMapping(value = "/upload", method = {RequestMethod.POST, RequestMethod.GET}) public List<String> upload(HttpServletRequest request) throws MultipartException { String path = ""; List<String> ksfs = new ArrayList<>(); //1 需傳imgUrl 其他值就是傳file文件上傳 try { //將當前上下文初始化給 CommonsMutipartResolver (多部分解析器) CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); //檢查form中是否有enctype="multipart/form-data" if (multipartResolver.isMultipart(request)) { //將request變成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; //獲取multiRequest 中所有的文件名 Iterator<String> iter = multiRequest.getFileNames(); while (iter.hasNext()) { //多個文件上傳 List<MultipartFile> fileList = multiRequest.getFiles(iter.next()); for (MultipartFile multipartFile : fileList) { String realPath = uploadFolder; String trueFileName = System.currentTimeMillis() + "." + FilenameUtils.getExtension(multipartFile.getOriginalFilename()); // 設置存放圖片文件的路徑 path = realPath + trueFileName; File file = new File(path); multipartFile.transferTo(file); path = reurl + "/upload/" + trueFileName; ksfs.add(path); } //單個文件 /* MultipartFile multipartFile=multiRequest.getFile(iter.next().toString()); if (multipartFile != null) { excelFile = File.createTempFile(prefix, ".jpg"); multipartFile.transferTo(excelFile); path = KSFileSave.uploadFile(prefix, excelFile, "jpg"); ksfs.add(path); }*/ } } } catch (Exception e) { e.printStackTrace(); } finally { } return ksfs; } }
調用接口測試,用如下的html頁面
<form method="POST" enctype="multipart/form-data" action="http:127.0.0.1:9090/test/upload"> <table> <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr> <tr><td></td><td><input type="submit" value="Upload" /></td></tr> </table> </form>
點擊選擇文件上傳驗證返回結果
查看上傳目錄
點擊返回接口訪問
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Spring Boot 2.x 實現文件上傳功能
- 解決feign微服務間的文件上傳報錯問題
- springBoot項目中使用@Value取值出現的問題及解決
- SpringBoot項目 文件上傳臨時目標被刪除異常的處理方案
- spring boot實現文件上傳