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

来自ling
跳转至: 导航搜索

https://blog.csdn.net/Dragon_MD/article/details/79745103?utm_source=blogxgwz6

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

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




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();
		}
	}
}