综合网上博客,最终版终结:spring cloud Feign 上传文件 !!

 

 

这两天在做一个 Feign 上传文件的需求,spring cloud官网上说 Feign 不支持 form 表单上传文件,所以我去了各大博客网站,综合看了很多相关的博客,走了很多弯路,也跳了很多坑,还好最终实现了自己的需求,所以在此写上博客记录下。 

我在项目中使用的 feign 版本是 2.0.4,对应openFeign.form 版本是3.3.0,所以各位看官结合自己的版本做参考吧!

openfeign版本
我所用的 open feign 版本

 

一、 关于引用依赖的问题

网上很多博客都说需要引用  feign-form 、feign-form-spring 依赖,大概 就像下面这样子:

    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form-spring</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>

但是我发现在spring-cloud-starter-openfeign 2.0.4 依赖中,已经自动添加了相关的依赖,所以我在项目中并没有引用上面这些依赖,只是引用了openfeign 依赖而已,如下图:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

 

二、重新注册SpringFormEncoder

这个我就直接贴代码了:

package com.xxx.xxx.config;


import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

/**
 * Feign 配置上传 Multipart 文件
 * @author zenglei
 *
 * 在 @FeignClient 注解中使用configure 引入
 */
@Configuration
public class FeignMultipartSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
}

另外,关于异常class XXX is not a type supported by this encoder.,请务必在FeignMultipartSupportConfig中注入messageConverters。

 

三、controller层使用

  controller 消费方,代码跟普通mvc一样接参就行,注意 @RequestParam 中 value 值要与 input 标签的 name 值一致!代码如下:

    @ApiOperation(value = "文件-上传")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "newFile", value = "文件", dataType = "MultipartFile", paramType = "File", example = ""),
        @ApiImplicitParam(name = "fileType", value = "文件类型", dataType = "String", paramType = "String", example = "xlsx")
    })
    @PostMapping(value = "/saveFile")
    public Resp<Boolean> saveFile(@RequestParam(value = "newFile") MultipartFile newFile
            , @RequestParam(value = "fileType") String fileType){
        return ok(webInfoRoomApi.saveFile(newFile, fileType));
    }

 

 四、@FeignClient  接收文件

在 @FeignClient 注解中,configuration属性引入之前配置重新注册 SpringFormEncoder 的 FeignMultipartSupportConfig ,代码如下:

@FeignClient(name = "provider-inforoom", configuration = {FeignAutoConfiguration.class, FeignMultipartSupportConfig.class})
public interface WebInfoRoomApi {
    @PostMapping(value = "/saveFile"
            , consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Boolean saveFile(@RequestPart(value = "newFile") MultipartFile newFile
            , @RequestParam(value = "fileType") String fileType);
}

@PostMapping  中,配置 consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE ,这是在大多数相关博客都有提到的,指定上传类型为form 表单。

saveFile 接口方法中,注意使用 @RequestPart 注解获取文件,其他参数使用 @RequestParam 即可。

 

实现类代码:

    @Override
    @PostMapping("/saveFile")
    public Boolean saveFile(@RequestPart(value = "newFile") MultipartFile newFile
            , @RequestParam(value = "fileType") String fileType) {

        return filesService.save(newFile,fileType);
    }

 

 

到此我这边就能成功的在 WebInfoRoomApi 接口 的实现类中获取到 MultipartFile 文件啦,使用 transferTo 方法就能把文件保存到指定的磁盘路径中。

 

其实碰到问题时,百度出来的技术博客那么多,真正能帮到自己项目的,还是需要自己去辨识和实践。上面就是本人解决自己项目中的feign  传递文件问题的步骤和代码,如果对你碰到的问题提供了解决思路,不妨给此博客点个赞哟!! 

 

 学无止境,生生不息。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值