綜合網上博客,最終版終結: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  傳遞文件問題的步驟和代碼,如果對你碰到的問題提供瞭解決思路,不妨給此博客點個贊喲!! 

 

 學無止境,生生不息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章