Springcloud项目中使用Feign传递MultipartFile对象

一、两个项目准备

web-boss 接收前端请求
service-base 处理相关业务

二、项目中的详细说明和配置

1.WEB-BOSS项目

(1) 增加Feign支持传递Multipart配置类
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration
public class FeignMultipartSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

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

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

(2) 处理前端请求的controller

POST请求接收上传文件请求,使用MultipartFile 类型接收前端<input type="file">的参数,其中是form表单形式提交的该参数,所以使用@RequestParam接收。

@RequestMapping(value = "/bankManage/batchImport" ,method = RequestMethod.POST)
    public BaseResMessage<List<Map<String,Object>>> fileUpload(@RequestParam("file") MultipartFile file){
        LOGGER.info("BankManageBatchController bankManage/uploadTemplate execute start ! ");
        BaseResMessage<List<Map<String,Object>>> resMsg = new BaseResMessage<>();
        try{
            if(file==null){
                throw new FileException(ServiceConstants.RES_SYSTEM_ERROR_CODE,"上传的文件不可为空");
            }
            resMsg = bankManageBatchClient.batchImportFromTemplate(file);
        }catch (Exception e){
            exceptionLogUtil.error(LOGGER, e);
            resMsg = new BaseResMessage<>(ServiceConstants.RES_SYSTEM_ERROR_CODE, e.getMessage());
        }finally {
            String data = "";
            if(resMsg.getData() != null){
                data = resMsg.getData().toString();
            }
            LOGGER.info("FileManageController /file/fileListUpload response code: {},message: {},data{}",resMsg.getData(),resMsg.getMessage(),data);
        }
         return resMsg;
(3)准备调用service-base的feignClient

其中使用@RequestPart注解,并且加上consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = MediaType.APPLICATION_JSON_UTF8_VALUE

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@FeignClient(name="service-base", fallback = BankManageBatchClient.HystrixClientFallback.class )
public interface BankManageBatchClient {

    /**
     * 批量导入
     * @return
     */
    @RequestMapping(method = RequestMethod.POST ,value = "/uploadBatImport",consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    BaseResMessage<List<Map<String,Object>>> batchImportFromTemplate(@RequestPart("file") MultipartFile file);
    /**
     * 内部类-断路器
     *
     */
    @Component
    static class HystrixClientFallback implements BankManageBatchClient{

        @Override
        public BaseResMessage<List<Map<String,Object>>> batchImportFromTemplate(MultipartFile file) {
            return new BaseResMessage<>(ServiceConstants.HYSTRIX_RES_CODE, ServiceConstants.HYSTRIX_RES_MESSAGE);
        }
    }
}

2.SERVICE-BASE项目

(1)Feign调用的Controller

同样使用@RequestPart接收MultipartFile

/**
     * 根据上传的excel文件进行批量导入
     * @param sessionId
     * @return
     */
    @RequestMapping(value = "/uploadBatImport",method = RequestMethod.POST)
    public BaseResMessage<List<Map<String,Object>>> uploadBatImport(@RequestPart MultipartFile file){
        LOGGER.info("BankManageBatAddController /uploadBatImport request Start! ");
        BaseResMessage<List<Map<String,Object>>> resMsg = new BaseResMessage<>();
        CacheSessionInfoDto user = null;
        try {
            resMsg = bankManageBatAddService.uploadBatImport(file);
        }catch (FileException e){
            exceptionLogUtil.error(LOGGER, e);
            resMsg = new BaseResMessage<>(e.getCode(), e.getMessage());
        } catch (Exception e){
            exceptionLogUtil.error(LOGGER, e);
            resMsg = new BaseResMessage<>(ServiceConstants.RES_SYSTEM_ERROR_CODE, e.getMessage());
        }
        LOGGER.info("BankManageBatAddController /uploadBatImport request end! ");
        return resMsg;
    }

三、测试结果

WEB-BOSS的Controller中接收到的MultipartFile实例对象
接收到的MultipartFile实例对象
SERVICE-BASE中接收到的MultipartFile实例对象
接收到的MultipartFile实例对象

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值