SpringCloud Feign实现MultipartFile文件上传 - 电光石火-引入包 compile(io.github.openfeign.form:feign-form:3.4.0) compile(io.g...

电光石火

陌晴博客
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文件上传

版权所有:《电光石火》 => SpringCloud Feign实现MultipartFile文件上传
本文地址:http://www.ilkhome.cn/?post=415
欢迎转载!请注明,文章均为 《电光石火》 原创,并注明本文地址,谢谢。

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(0)