一、两个项目准备
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实例对象
在SERVICE-BASE中接收到的MultipartFile实例对象