SpringCloudFeign多文件上传「微服务」

应用场景

第三方调用的我的接口,上传若干个文件,我用MultiPartFile[]数组接收,之后我调用其他服务的接口,把文件发送过去,统一上传保存

存在的问题

当你使用feign传递MultipartFile对象时,接收方无法解析,所以,需要重写feignencoder,让它支持MultipartFile类型以及MultipartFile[]数组类型


代码示例

我的接口如下

@ApiOperation(value = "影像上传", httpMethod = "POST")
@PostMapping(Url.IMAGE_UPLOAD)
public RestResponse imageUpload(
    @RequestPart(value = "files", required = false) MultipartFile[] files,
    @RequestParam(value = "type", required = false) String type) {
    return noCarInsureService.imageUpload(files, type);
}

我的FeignClient

@FeignClient(
    name = "tenbent-pb-insurer", 
    url = "http://xx.xx.xx.xx:9510", 
    configuration = FeignMultipartSupportConfig.class
)
public interface AeonLifeFeignClient {
    @RequestMapping(value = Url.IMAGE_UPLOAD, method = RequestMethod.POST,
            produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    RestResponse imageUpload(
            @RequestPart(value = "files", required = false) MultipartFile[] files,
            @RequestParam(value = "type", required = false) String type);
}

需要注意的地方

  1. 添加 consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  2. MultipartFile需要用 @RequestPart,不能用 @RequestParam,如果用 @RequestParam,被调用的服务方会出现如下错误:
Caused by: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

跨服务调用的方法,MultipartFile 的注解要用 @RequestPart

关于@RequestPart@RequestParam的区别:
当请求方法的请求参数类型不再是String类型的时候,@RequestParam依赖于注册Converter或者PropertyEditor来解析参数,而@RequestPart则是通过HttpMessageConverter来根据Content-Type决定采用的消息转换器。
总结:
@RequestParam适用于name-valueString类型的请求域
@RequestPart适用于复杂的请求域,例如:JSON、XML
3. 设置 ContentTypemultipart/form-data


解决办法

1. POM文件中引入以下两个jar包(让他支持任意数量文件上传)

<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.2.2</version>
</dependency>

<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>3.2.2</version>
</dependency>

2. 引用配置类FeignMultipartSupportConfig,并且实例化

@Configuration
public class FeignMultipartSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        // 为Encoder注入messageConverters
        return new SpringMultipartEncoder(new SpringEncoder(messageConverters));
    }
}

3. 源码改写

feign-form-spring包中有这样一个类:
SpringFormEncoder源码:

public class SpringFormEncoder extends FormEncoder {

  /**
   * Constructor with the default Feign's encoder as a delegate.
   */
  public SpringFormEncoder () {
    this(new Encoder.Default());
  }

  /**
   * Constructor with specified delegate encoder.
   *
   * @param delegate  delegate encoder, if this encoder couldn't encode object.
   */
  public SpringFormEncoder (Encoder delegate) {
    super(delegate);

    val processor = (MultipartFormContentProcessor) getContentProcessor(MULTIPART);
    processor.addWriter(new SpringSingleMultipartFileWriter());
    processor.addWriter(new SpringManyMultipartFilesWriter());
  }

  @Override
  public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {
    if (!bodyType.equals(MultipartFile.class)) {
      super.encode(object, bodyType, template);
      return;
    }

    val file = (MultipartFile) object;
    val data = singletonMap(file.getName(), object);
    super.encode(data, MAP_STRING_WILDCARD, template);
  }
}

从上面SpringFormEncoder的源码上可以看到SpringFormEncoder类构造时把SpringManyMultipartFilesWriter实例添加到了处理器列表里,但是在encode方法里又只判断了MultipartFile类型,没有判断数组类型,因些需要改写这个类(让它支持MultipartFileMultipartFile[]),将SpringFormEncoder类源码复制出来重命名为 FeignSpringFormEncoder,改写之后的源码如下:

public class FeignMultipartSpringFormEncoder extends FormEncoder {

    /**
     * Constructor with the default Feign's encoder as a delegate.
     */
    public FeignMultipartSpringFormEncoder() {
        this(new Default());
    }

    /**
     * Constructor with specified delegate encoder.
     *
     * @param delegate delegate encoder, if this encoder couldn't encode object.
     */
    public FeignMultipartSpringFormEncoder(Encoder delegate) {
        super(delegate);

        val processor = (MultipartFormContentProcessor) getContentProcessor(MULTIPART);
        processor.addWriter(new SpringSingleMultipartFileWriter());
        processor.addWriter(new SpringManyMultipartFilesWriter());
    }

    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        if (bodyType.equals(MultipartFile.class)) {
            // 单MultipartFile判断
            val file = (MultipartFile) object;
            val data = singletonMap(file.getName(), object);
            super.encode(data, MAP_STRING_WILDCARD, template);
            return;

        } else if (bodyType.equals(MultipartFile[].class)) {
            // MultipartFile数组处理
            val file = (MultipartFile[]) object;
            if (file != null) {
                val data = singletonMap(file.length == 0 ? "" : file[0].getName(), object);
                super.encode(data, MAP_STRING_WILDCARD, template);
                return;
            }
        }
        // 其他类型调用父类默认处理方法
        super.encode(object, bodyType, template);
    }
}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值