feign多文件上传

时间:2021-06-17 07:25:04

参考地址:https://www.cnblogs.com/standup/p/9090113.html

     https://www.cnblogs.com/standup/p/9093753.html

1、服务消费方pom引用Feign-form依赖:

    <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>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>

2、服务消费方声明一个“FeignSpringFormEncoder”类(这个类copy自”SpringFormEncoder”接口):

import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.ContentType;
import feign.form.FormEncoder;
import feign.form.MultipartFormContentProcessor;
import feign.form.spring.SpringManyMultipartFilesWriter;
import feign.form.spring.SpringSingleMultipartFileWriter;
import org.springframework.web.multipart.MultipartFile; import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Map; /**
* author:YZH
* time: 2019/4/29 15:35
* description: 多文件上传配置
**/
public class FeignSpringFormEncoder extends FormEncoder {
public FeignSpringFormEncoder() {
this(new Default());
}
public FeignSpringFormEncoder(Encoder delegate) {
super(delegate);
MultipartFormContentProcessor processor = (MultipartFormContentProcessor)this.getContentProcessor(ContentType.MULTIPART);
processor.addWriter(new SpringSingleMultipartFileWriter());
processor.addWriter(new SpringManyMultipartFilesWriter());
} public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
if (bodyType.equals(MultipartFile.class)) {
MultipartFile file = (MultipartFile) object;
Map<String, Object> data = Collections.singletonMap(file.getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
return;
} else if (bodyType.equals(MultipartFile[].class)) {
MultipartFile[] file = (MultipartFile[]) object;
if(file != null) {
Map<String, Object> data = Collections.singletonMap(file.length==0?"":file[0].getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
return;
}
}
super.encode(object, bodyType, template);
}
}

3、将“FeignSpringFormEncoder”作为bean提供给框架,代替“SpringFormEncoder”的“encode()”接口:

import feign.codec.Encoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope; /**
* author:YZH
* time: 2019/4/29 15:37
**/
public class FeignMultipartSupportConfig {
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new FeignSpringFormEncoder();
}
}

4、feign调用

import com.example.eurekaclient.conf.FeignMultipartSupportConfig;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; /**
* author:YZH
* time: 2018/6/14 10:59
**/
@FeignClient(value = "service-feign",configuration = FeignMultipartSupportConfig.class)
public interface Hiservice { @RequestMapping(value = "/other")
String test(@RequestParam("name")String name,@RequestParam("age")int age); @PostMapping(value = "uploadFiles",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String uploadFiles(@RequestPart(value = "files")MultipartFile[] files,@RequestParam(value = "time") String time); @PostMapping(value = "upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String upload(@RequestPart(value = "file")MultipartFile file);
}

5、服务消费方控制层

feign多文件上传