前面做了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进行了测试: