SpringCloud Feign实现MultipartFile文件上传

2019-12-05 18:32:03 浏览数 (1)

引入包

代码语言:javascript复制
compile("io.github.openfeign.form:feign-form:3.4.0")
compile("io.github.openfeign.form:feign-form-spring:3.4.0")
compile('commons-fileupload:commons-fileupload:1.3.3')

在Feign Client中引用配置类FeignMultipartSupportConfig,在FeignMultipartSupportConfig中,实例化FeignSpringFormEncoder。 注意:FeignClient中注解@PostMapping的produeces和consumes不能少。

代码语言:javascript复制
package com.test.core.bean.config;
 
 import feign.codec.Encoder;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.multipart.commons.CommonsMultipartResolver;

 @Configuration
 public class FeignMultipartSupportConfig {
 
     @Bean
     public Encoder encoder(){
         return new FeignSpringFormEncoder();
     }
 
     @Bean
     public feign.Logger.Level multipartLoggerLevel() {
         return feign.Logger.Level.FULL;
     }
 
     @Bean
     public CommonsMultipartResolver commonsMultipartResolver() {
         final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
         commonsMultipartResolver.setMaxUploadSize(20*1024*1024);
         return commonsMultipartResolver;
     }
 }
代码语言:javascript复制
 package com.test.core.bean.config;
 
 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.io.File;
 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.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", object);
                 super.encode(data, MAP_STRING_WILDCARD, template);
                 return;
             }
         } else if (bodyType.equals(File[].class)) {
             File[] file = (File[]) object;
             if (file != null) {
                 Map<String, Object> data = Collections.singletonMap("file", object);
                 super.encode(data, MAP_STRING_WILDCARD, template);
                 return;
             }
         } else if (bodyType.equals(File.class)) {
             File file = (File) object;
             Map<String, Object> data = Collections.singletonMap("file", object);
             super.encode(data, MAP_STRING_WILDCARD, template);
             return;
         }
         super.encode(object, bodyType, template);
     }
 }

修改feign文件

代码语言:javascript复制
 @FeignClient(value = "ossServer", fallback = OSSServiceFallback.class, configuration = {FeignOauth2RequestInterceptor.class, FeignMultipartSupportConfig.class})
 public interface OSSServiceApi {
 
     /**
      * 上传文件
      *
      MockMultipartFile mockMultipartFile    = null;
      try {
      mockMultipartFile = new MockMultipartFile("file", "1.txt", "txt", new FileInputStream(new File("D://1.txt")));
      } catch (IOException e) {
      e.printStackTrace();
      }
      System.out.println(ossServiceApi.upload(mockMultipartFile).toString());
      * @param file
      * @return
      */
     @PostMapping(value = "/api/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     ResponseBean<String> upload(@RequestPart("file") MultipartFile file);

修改feign回调内容

代码语言:javascript复制
     @Override
     public ResponseBean<String> upload(@RequestPart("file") MultipartFile file){
         log.error("调用 {} 异常: {}", "file", file.getOriginalFilename());
         return new ResponseBean<>(ResponseBean.FAIL, "OSSServiceFallback调用异常 :"   file.getOriginalFilename(), null);
     }

0 人点赞