springCloud微服务之间传输文件

20200702更新

鉴于最近有朋友不能成功实现,我这里补充一点东西。

spring-cloud-starter-openfeign 在2.0.2.RELEASE版本后,已经集成了feign-form-spring 以及 feign-form,因此此版本以后不需再额外添加依赖包,也不再需要添加以下的MultipartSupportConfig 的配置文件。

如图

用法很简单,跟下面内容说的一样。

依赖:

 
  1. <dependency>

  2. <groupId>org.springframework.cloud</groupId>

  3. <artifactId>spring-cloud-starter-openfeign</artifactId>

  4. <version>2.0.2.RELEASE</version>

  5. </dependency>

调用方(Feign客户端):

 
  1. @PostMapping(value = "xxx",consumes = MediaType.MULTIPART_FORM_DATA_VALUE )

  2. ResponseVO<String> xxxx(@RequestPart("file") MultipartFile file);

被调用方:

 
  1. @PostMapping(value = "xxx")

  2. public ResponseVO<String> xxxx(@RequestParam("file") MultipartFile file){

  3. //xxxx

  4. }

这样写法即可,已经测试过没问题,注意下RequestParam 与 RequestPart的区别。

 

-----------------原文---------------------

一般我们上传文件,都会利用MultipartFile进行文件的上传。之前我做数据导入的时候就遇到了问题,项目使用

springcloud,在服务与服务之间调用时,发现MultipartFile上传文件会报错,被调用服务会认为该请求是不合法的文件上传请

求,后来查找资料发现是feign会把文件追加到url上,而且请求头中的contenttype不是文件上传类型,我尝试过在注解中

(@RequestMapping)添加headers属性,把contenttype设置为multipart/form-data,还是会有报错。之后网上查资料,发现大

家的思路都差不多,但是我都没有成功,后来一直调试终于给搞定。很简单,就几个步骤。

 

首先在pom文件导入依赖包(我用的feign)

 
  1. <dependency>

  2. <groupId>io.github.openfeign.form</groupId>

  3. <artifactId>feign-form</artifactId>

  4. <version>2.1.0</version>

  5. </dependency>

  6. <dependency>

  7. <groupId>io.github.openfeign.form</groupId>

  8. <artifactId>feign-form-spring</artifactId>

  9. <version>2.1.0</version>

  10. </dependency>

 然后编写配置类

 
  1. import feign.codec.Encoder;

  2. import feign.form.spring.SpringFormEncoder;

  3. import org.springframework.beans.factory.ObjectFactory;

  4. import org.springframework.beans.factory.annotation.Autowired;

  5. import org.springframework.boot.autoconfigure.web.HttpMessageConverters;

  6. import org.springframework.cloud.netflix.feign.support.SpringEncoder;

  7. import org.springframework.context.annotation.Bean;

  8. import org.springframework.context.annotation.Configuration;

  9.  
  10. /**

  11. * @author chenws

  12. * @decription

  13. * @date 2018/7/25

  14. */

  15. @Configuration

  16. public class MultipartSupportConfig {

  17.  
  18. @Autowired

  19. private ObjectFactory<HttpMessageConverters> messageConverters;

  20.  
  21. @Bean

  22. public Encoder feignFormEncoder() {

  23. return new SpringFormEncoder(new SpringEncoder(messageConverters));

  24. }

 最后再编写服务发现

 
  1. /**

  2. * @author chenws

  3. * @date 2018/7/10

  4. */

  5. @FeignClient(name="xxxx", configuration = MultipartSupportConfig.class)

  6. public interface XXXClient {

  7.  
  8. @RequestMapping(value = "/xxx",method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

  9. ResponseVO xxx(@RequestPart("file") MultipartFile file);

   }

 注意标红的地方

configuration = MultipartSupportConfig.class:配置第一步编写的类

consumes = MediaType.MULTIPART_FORM_DATA_VALUE:设置该请求为文件上传请求

RequestPart:一定要用RequestPart,而不能用RequestParam,不然会报错。

注意:被调用的服务用@RequestParam,不是@RequestPart

 

 

作者:碩果
链接:https://blog.csdn.net/shuoshuo132
来源:CSDN
著作权归作者所有,任何形式的转载都请注明出处。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值