springCloud 传文件

假如想象 提交于 2019-12-04 20:43:25
依赖<dependency>    <groupId>io.github.openfeign.form</groupId>    <artifactId>feign-form</artifactId>    <version>3.8.0</version></dependency><dependency>    <groupId>io.github.openfeign.form</groupId>    <artifactId>feign-form-spring</artifactId>    <version>3.8.0</version></dependency>
服务消费者Service接口
@FeignClient(value = "xqwl-street-provider", configuration = ImsRhinfoZyxqCarService.MultipartSupportConfig.class)@Servicepublic interface ImsRhinfoZyxqCarService {    @PostMapping(value = "/ImsRhinfoZyxqSanitationController/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)    R SanitationUpload(@RequestPart(value = "file")MultipartFile file);    @Configuration    class MultipartSupportConfig {        @Bean        public Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {            return new FeignSpringFormEncoder(new SpringEncoder(messageConverters));        }    }}传文件配置
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;
public class FeignSpringFormEncoder extends FormEncoder {    public FeignSpringFormEncoder() {        this(new Default());    }    public FeignSpringFormEncoder(Encoder delegate) {        super(delegate);        MultipartFormContentProcessor processor = (MultipartFormContentProcessor)this.getContentProcessor(ContentType.MULTIPART);        processor.addFirstWriter(new SpringSingleMultipartFileWriter());        processor.addFirstWriter(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);    }}服务提供者接口
@PostMapping(value="upload",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,        consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public R upload(MultipartFile file){  return   sanitationService.importExcel(file);}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!