前面做了Feign調用文件下載服務接口的例子,這里順帶把Feign調用文件上傳服務接口的例子也一起做了!一樣直接上代碼:
首先是文件上傳服務提供者的主要代碼:
@PostMapping(value = "/upload") public String uploadFile(@RequestPart MultipartFile file) { if (file.isEmpty()) { return null; } String fileName = file.getOriginalFilename(); String storePath = "C:\\java\\upload";//換成你自己的文件上傳存儲目錄 File destFile = new File(storePath + "\\" + fileName); if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs();//注意,是mkdirs不是mkdir,后者只創建文件的直接父目錄,創建不了多層目錄 } try { file.transferTo(destFile); return destFile.getAbsolutePath();//返回文件上傳成功后的文件存儲完整路徑 } catch (IOException e) { e.printStackTrace(); return null; } }
接着我們在Postman中先測試一下上述接口是否可用(Postman中使用POST請求,在Body中選擇form-data格式,將Key的參數類型切換成File,在Value中選擇目標文件):
可以看到上傳成功了,接着我又試着上傳一個大一點的文件,上傳了一個電影,結果翻車了:
查看IDEA控制台的報錯輸出看到以下信息:
2020-07-15 17:49:54.227 ERROR 5936 --- [nio-8001-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (1538829581) exceeds the configured maximum (10485760)] with root cause org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (1538829581) exceeds the configured maximum (10485760)
看到重要信息——SizeLimitExceededException異常,超出大小限制異常!好吧,查看Spring Boot源碼發現文件下載默認單文件最大是1MB,單次多個文件總大小最大是10MB,這也太小了!
在application.yml中修改一下配置,加大電流!將單文件下載的最大尺寸設為3GB,單次多個文件總大小最大尺寸設為10GB:
spring: servlet: multipart: max-file-size: 3GB max-request-size: 10GB
重新啟動服務提供者,再次測試上傳剛才1.43GB的電影:
現在服務提供者的文件上傳服務接口就已經准備好了!
接下來是服務消費者(Feign客戶端)的代碼:
新版本的Feign聲明文件上傳接口也已經很簡單了,下面是我的Feign依賴信息:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.3.RELEASE</version> </dependency>
Feign接口聲明:
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.multipart.MultipartFile; @FeignClient(name = "provider-user") @RequestMapping(value = "/user") public interface UserFeignClient extends FeignClientParent { @PostMapping(value = "/upload",produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE,consumes = MediaType.MULTIPART_FORM_DATA_VALUE) String uploadFile(@RequestPart MultipartFile file); }
然后是服務消費者的Controller接口:
@PostMapping(value = "/upload", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String uploadFile(@RequestPart MultipartFile file) { return this.userFeignClient.uploadFile(file); }
這里特別提醒一下:首先一定要注意上述代碼中標紅標粗的produces、consumes和@RequestPart注解,特是@RequestPart注解不要寫成了@RequestParam,否則你會翻車的!
另外,Feign調用上傳文件服務接口是借助了feign-form和feign-form-spring組件,早期的Feign包中不包含這兩個組件,需有額外引用,但現在較新版本的Feign內部都集成了這兩個依賴,不用再額外引用了!
接着我就先后跑起了Eureka服務端→文件上傳服務提供者端→文件上傳服務消費者端,然后輕輕松松的打開Postman進行了測試: