Feign调用文件上传服务接口样例
前面做了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进行了测试:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!