Feign进行跨服务传递MultipartFile文件

1.编写目的

通过调用服务进行文件上传,避免每个需要上传文件的模块都写一遍上传服务,造成代码冗余。

2.主要内容

本文主要包含通过feign进行文件上传模块。

3.作用

使技术人员在开发过程中遇到问题时有地可查,有章可循。

4.如何通过feign进行跨服务传递MultipartFile文件

4.1添加依赖

<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form</artifactId>
   <version>3.0.3</version>
</dependency>
<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form-spring</artifactId>
   <version>3.0.3</version>
</dependency>

4.2添加配置文件

 1 package com.ruiyi.twowayreferral.configurer;
 2 
 3 import feign.codec.Encoder;
 4 import feign.form.spring.SpringFormEncoder;
 5 import org.springframework.beans.factory.ObjectFactory;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
 8 import org.springframework.cloud.openfeign.support.SpringEncoder;
 9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Configuration;
11 
12 @Configuration
13 public class MultipartSupportConfig {
14 
15     @Autowired
16     private ObjectFactory<HttpMessageConverters> messageConverters;
17 
18     @Bean
19     public Encoder feignFormEncoder() {
20         return new SpringFormEncoder(new SpringEncoder(messageConverters));
21     }
22 }

4.3代码示例

 1 @FeignClient(value = "controller-center")
 2 public interface CallFrignService {
 3 
 4     /**
 5      * @Create 文件上传 wanggx_ruiyi 2019.11.15
 6      * @param uploadPath 文件上传地址
 7      * @param file 上传的文件
 8      * @return
 9      */
10     @PostMapping(value = "/api/v1/common/file/fileUpload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
11     String fileUpload(@RequestParam(value = "uploadPath", required = true) String uploadPath,@RequestPart(value = "file", required = true) MultipartFile file);
12 }
原文地址:https://www.cnblogs.com/wgx519/p/13953586.html