feign不能正常传递参数MultipartFile(文件)时的解决手段

之前工作中有个业务场景:需要把文件以MultipartFile进行服务之间的调用(使用的是Feign),特此记录下踩到的坑

需要注意的是:使用默认的springcloud自带的spring-cloud-starter-openfeign是不支持传递文件的。

我看网上有很多使用feign-form和feign-form-spring支持的jar包,然后配置SpringFormEncoder,但我试了,没有成功,最后使用了另外一种方法,在此记录下吧:

方法一:使用外部依赖

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

新建配置类:

package com.jsyd.ict.ictservicemanager.configuration;

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;

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

/**
 * 处制定了注入的feignFormEncoder可以覆盖掉原本的encoder
 * 该方式可以让发送multipartFile成为可能
 * @author wangyang
 * @version 1.0
 * @date 2021/4/13 19:13
 */
@Configuration
public class MultipartSupportConfig {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

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

配置feign接口:

package com.jsyd.ict.ictservicemanager.feign;

import com.jsyd.ict.ictservicemanager.util.resp.RespUtil;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;

/**
 * 能力的调用
 *
 * @author wangyang
 * @version 1.0
 * @date 2021/3/11 21:01
 */
@FeignClient(name = "ict-service-capacity", configuration = MultipartSupportConfig.class)
public interface CapacityFeign {
    @RequestMapping(value = "contentAudit/baiduImgAuditByFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    RespUtil baiduImgAuditByFile(@RequestPart(value = "file") MultipartFile file);
}

注意:文件的传输需要使用@RequestPart()注解

服务端:

    @RequestMapping(value = "/baiduImgAuditByFile", method = RequestMethod.POST)
    public RespUtil baiduImgAuditByFile(@RequestParam("file") MultipartFile file)  {
		// 调用service
        return null;
    }

结果发现,这样是获取不到结果的,研究了好一会,因为要着急联调,就没有继续跟踪了,有时间在深入研究下。

后来使用了下面的方法,成功传参

方法二:使用HttpServletRequest获取

上述操作都不需要,即:不需要导入依赖,也不需要创建文件,只需要在服务端通过HttpServletRequest去设置

使用HttpServletRequest设置:

伪代码如下:

    @RequestMapping(value = "/baiduImgAuditByFile", method = RequestMethod.POST)
    public RespUtil baiduImgAuditByFile(MultipartFile file, HttpServletRequest request) {
        if (file == null) {
            // 解决服务间调用无法接受MultipartFile参数的问题
            String contentType = request.getContentType();
            if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
                MultipartHttpServletRequest multipartRequest =
                        WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
                if (multipartRequest != null) {
                    MultiValueMap<String, MultipartFile> multiFileMap = multipartRequest.getMultiFileMap();
                    for (Map.Entry<String, List<MultipartFile>> entry : multiFileMap.entrySet()) {
                        file = entry.getValue().get(0);
                    }
                }
            }
        }
       	// 处理业务逻辑
        return null;
    }

主要说明如下图:在这里插入图片描述

即通过HttpServletRequest 的方式即使文件获取不到,也可以进行特殊处理,从而获取文件。

以上就是通过feign传参MultipartFile时遇到的小问题,特此记录下,如果有小伙伴有更好的思路或者发现第一种方案有错误或遗漏时,麻烦提出来,一起沟通,一起学习…

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值