在我的写的方法中.涉及到了跨服务上传文件的方法,但是这个时候经过尝试跨服务无法传递文件
本来我是已经放弃了,改成了数组传递.不过后面又找到了可以支持传递文件的方式
方法如下:
最开始的时候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();
}
}
}