Spring boot + Spring Cloud框架下通过Feign进行跨服务传递MultipartFile文件

在我的写的方法中.涉及到了跨服务上传文件的方法,但是这个时候经过尝试跨服务无法传递文件

本来我是已经放弃了,改成了数组传递.不过后面又找到了可以支持传递文件的方式

方法如下:

最开始的时候Spring cloud中,Feign本身是不支持上传文件的能力的,估约1年前.

要想实现这个方法,需要自己去编写Encoder的,然而这种情况编写起来比较复杂麻烦.

我也在网上看过编写Encoder的方式,情况比较麻烦.

所幸,在寻找的路途中发现了Feign官方提供了上传子项目, "feign-form ".其中就实现了我们所需要的Encoder.

首先先添加依赖:

<dependency>

<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>

然后我们再修改Feign Client类:

package com.zeunpro.re.biz.disk.client;
 
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
 
import com.zeunpro.re.biz.common.constant.UserConstant;
import com.zeunpro.re.biz.common.msg.RspMessage;
 
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import io.swagger.annotations.ApiOperation;
 
@FeignClient(name = UserConstant.ZP_SERVICE_FILE_UPLOAD, fallback = FileuploadCilentFallback.class,configuration = FileuploadClient.MultipartSupportConfig.class)
public interface FileuploadClient {
	
	/**
	 * 跨服务调用的方法,注意MultipartFile的注解要用@RequestPart
	 */
	@ApiOperation(httpMethod = "PUT", value = "上传文件-MultipartFile", notes = "上传文件-MultipartFile")
	@PutMapping(value = "/fileupload/upload/file/by/multipartFile", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public RspMessage uploadFile(@RequestPart(value = "file", required = false) MultipartFile file, @RequestParam(value = "whetherTranscoding", required = false, defaultValue = "false") boolean whetherTranscoding, @RequestParam(value = "url", required = false) String url,@RequestParam(value = "skip", required = false, defaultValue = "0") long skip);
	
	/**
	 * 引用配置类MultipartSupportConfig.并且实例化
	 */
	class MultipartSupportConfig {
		@Bean
		public Encoder feignFormEncoder() {
			return new SpringFormEncoder();
		}
	}
}

此处需要注意的是跨服务传递的方法中不能用以前的RequestParam,需要用RequestPart来注解

样例类:

/**
 * @Created by ****
 * @DateTime: ****.
 * @Desription: FTP文件上传和下载接口
 */
@FeignClient(value = "**", configuration = UpDownFtpFacade.MultipartSupportConfig.class)
public interface UpDownFtpFacade {

    /**
     * FTP上传文件
     *
     * @param file  文件
     * @param logId 日志id
     * @return
     */
    @PostMapping(value = "/ftp/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    FtpApiResponse<FtpUploadResDTO> uploadFileFTP(@RequestPart(value = "file") MultipartFile file,
                                                  @RequestParam("logId") String logId);

    /**
     * FTP下载文件
     *
     * @param fileName 文件名
     * @param logId    日志id
     * @return
     */
    @PostMapping(value = "/ftp/downloadFile")
    FtpApiResponse<String> downloadFileFTP(@RequestParam("fileName") String fileName,
                                           @RequestParam("logId") String logId);

    /**
     * 引用配置类MultipartSupportConfig.并且实例化
     */
    @Configuration
    class MultipartSupportConfig {
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }

}

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值