电光石火电光石火

陌晴博客
ilkhome.cn

SpringCloud Feign实现MultipartFile文件上传

引入包

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不能少。

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;
     }
 }
 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文件

 @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回调内容

     @Override
     public ResponseBean<String> upload(@RequestPart("file") MultipartFile file){
         log.error("调用 {} 异常: {}", "file", file.getOriginalFilename());
         return new ResponseBean<>(ResponseBean.FAIL, "OSSServiceFallback调用异常 :" + file.getOriginalFilename(), null);
     }
 
本博客所有文章如无特别注明均为原创。作者:似水的流年
版权所有:《电光石火》 => SpringCloud Feign实现MultipartFile文件上传
本文地址:http://www.ilkhome.cn/index.php/archives/415/
欢迎转载!复制或转载请以超链接形式注明,文章为 似水的流年 原创,并注明原文地址 SpringCloud Feign实现MultipartFile文件上传,谢谢。

评论

  1. I think that what you posted was actually very reasonable.

    But, what about this? what if you added a little content?
    I ain't saying your information isn't good., but what if you added a title that grabbed folk's attention? I mean SpringCloud Feign实现MultipartFile文件上传 - 电光石火-引入包 compile(io.github.openfeign.form:feign-form:3.4.0) compile(io.g...
    is a little vanilla. You should peek at Yahoo's home page and note how
    they create article titles to grab viewers interested. You might add a video or a related pic or two to get
    people interested about everything've got to say. In my opinion, it would
    bring your posts a little bit more interesting.

    my homepage: [1](https://trinhcongson.info/index.php/blog/8243/how-to-download-pdf-to-word-converter-for-free-download-free-pdf-to-word-do/ "1")

    OH 2019-03-05    回复