FeignCilent转发文件上传时遇到的一系列问题

FeignCilent转发文件上传时遇到的一系列问题

这个问题在网上查到了一些资料,这里做下总结。
参考:

https://www.jianshu.com/p/3ecbc0062411
https://blog.csdn.net/qq_35307947/article/details/105097870

问题

1、服务端获取不到文件
2、获取文件失败
the request was rejected because no multipart boundary was found

原因

feignClient转发MultipartFile时content-type没有boundary,下面的源码报错。

protected byte[] getBoundary(String contentType) {
        ParameterParser parser = new ParameterParser();
        parser.setLowerCaseNames(true);
        // Parameter parser can handle null input
        Map<String, String> params = parser.parse(contentType, new char[] {';', ','});
        String boundaryStr = params.get("boundary");

        if (boundaryStr == null) {
            return null;
        }
        byte[] boundary;
        try {
            boundary = boundaryStr.getBytes("ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            boundary = boundaryStr.getBytes(); // Intentionally falls back to default charset
        }
        return boundary;
    }

处理

调用方配置自定义FeignConfig。定义multipart/form-data。改用@RequestPart

主要改动代码

调用方
@FeignClient(value = "xxx", configuration = FeignConfig.class)
public interface UserClientService {

    @PostMapping(path = "/manager/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResultVO managerImport(@RequestPart("file") MultipartFile multfile);
}
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;

public class FeignConfig{
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}

pom依赖

		<dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
服务方
@PostMapping("/manager/import")
    public ResultVO managerImport(@RequestPart("file") MultipartFile multfile) {
        // TODO
    }

后记

另外还查到一种方式是用 @RequestLine,但是没成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值