springcloud feign 实现文件上传

更多内容请浏览本人博客

文件上传服务服务本来并没有什么难度,但是在spring cloud中使用feign调用时会出现一大堆莫名奇妙的bug,现记录一个可以使用的解决方案

  1. 引入文件feign文件上传依赖

            io.github.openfeign.form
      feign-form
      3.0.3
 

            io.github.openfeign.form
            feign-form-spring
            3.0.3

  1. 编写feign文件上传配置文件
package com.huidong.qzh.standard.config;

import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import feign.codec.Encoder;

@Configuration
public class FeignMultipartSupportConfig {
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder();
    }

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
}

  1. 编写feign客户端
package com.huidong.qzh.standard.feign;

import com.huidong.qzh.standard.config.FeignMultipartSupportConfig;
import com.huidong.qzh.util.common.util.QzhResult;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

@FeignClient(value = "qzh-file", configuration = FeignMultipartSupportConfig.class)
public interface FILEFeignClient {
    @RequestMapping(method = RequestMethod.POST, value = "/uploadFile",
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public QzhResult handFileUpload(@RequestPart(value = "file") MultipartFile file, @RequestParam(value = "path") String path);


}

你可能感兴趣的:(springcloud feign 实现文件上传)