spring cloud feign client 上传文件遇到问题记录

Posted zjhgx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud feign client 上传文件遇到问题记录相关的知识,希望对你有一定的参考价值。

项目中用FeignClient上传文件时,发现大小无法大于1M,代码如下:

MultiValueMap<String, String> requestMap = new LinkedMultiValueMap<String, String>();
OptUploadFileByteInfoReqDto optUploadFileByteInfoReqDto = new OptUploadFileByteInfoReqDto();
optUploadFileByteInfoReqDto.setFileByteArray(file.getBytes());
optUploadFileReqDto.setUploadFileByteInfoReqDto(optUploadFileByteInfoReqDto);
requestMap.add("file", JSON.toJSONString(optUploadFileReqDto));

// TODO 待修改,增加loginAuthDto
response = opcRpcService.uploadFile(file, null);
------------------------------

@Transactional(rollbackFor = Exception.class)
public OptUploadFileRespDto uploadFile(MultiValueMap<String, String> requestMap, LoginAuthDto loginAuthDto) {
Wrapper<OptUploadFileRespDto> response = opcOssFeignApi.uploadFile(requestMap);
log.info("----- 上传文件响应:{}", response);
if (response != null && response.success()) {
OptUploadFileRespDto result = response.getResult();
MdcAttachment mdcAttachment = new MdcAttachment();
mdcAttachment.setAttachmentNo(result.getRefNo());
mdcAttachment.setBucketName(result.getAttachmentPath());
mdcAttachment.setRefNo(result.getRefNo());
mdcAttachment.setName(result.getAttachmentName());
mdcAttachment.setPath(result.getAttachmentUrl());
mdcAttachment.setType(result.getFileType());
mdcAttachment.setFormat(result.getFileType());
// TODO 待修改,去除非空判断
mdcAttachment.setCreator(loginAuthDto != null ? loginAuthDto.getLoginName() : null);
mdcAttachment.setCreatorId(loginAuthDto != null ? loginAuthDto.getAccountId() : null);
mdcAttachment.setCreatedTime(LocalDateTime.now());
mdcAttachment.setUpdateTime(LocalDateTime.now());
mdcAttachmentMapper.insertSelective(mdcAttachment);
result.setAttachmentId(mdcAttachment.getId());
return result;
}
return null;
}
------------------------------
public class OpcAttachmentFeignClient extends BaseController implements OpcOssFeignApi {

@Resource
private OpcAttachmentService opcAttachmentService; @Resource private OpcAliossService opcAliOssService; @Override @ApiOperation(httpMethod = "POST", value = "上传文件") public Wrapper<OptUploadFileRespDto> uploadFile(MultiValueMap<String, String> requestMap) { OptUploadFileReqDto optUploadFileReqDto = JSON.parseObject(requestMap.getFirst("file"), OptUploadFileReqDto.class); String fileType = optUploadFileReqDto.getFileType(); String fileName = LocalDateTime.now().format(DateUtil.DATE_FORMAT_30) + RandomUtil.createNumberCode(4) + "." + fileType; OptUploadFileRespDto optUploadFileRespDto = null; try { optUploadFileRespDto = opcAliOssService .uploadFile(optUploadFileReqDto.getUploadFileByteInfoReqDto().getFileByteArray(), fileName); } catch (IOException e) { e.printStackTrace(); logger.error("上传文件发生异常:{}", e.getMessage()); return WrapMapper.error("上传文件发生异常"); } if (optUploadFileRespDto != null) { optUploadFileRespDto.setFileType(fileType); } return WrapMapper.ok(optUploadFileRespDto); }

 当文件>1M时,被调用的uploadFile方法的requestMap就变成空了,原因不明。

于是换了种写法:

    @Transactional(rollbackFor = Exception.class)
    public OptUploadFileRespDto uploadFile(MultipartFile file, LoginAuthDto loginAuthDto) {

        Wrapper<OptUploadFileRespDto> response = opcOssFeignApi.handleFileUpload(file);
        log.info("----- 上传文件响应:{}", response);
        if (response != null && response.success()) {
            OptUploadFileRespDto result = response.getResult();
-------------
@Override
public Wrapper<OptUploadFileRespDto> handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
String fileName = LocalDateTime.now().format(DateUtil.DATE_FORMAT_30) + RandomUtil.createNumberCode(4) + "."
+ fileType;
OptUploadFileRespDto optUploadFileRespDto = null;
try {
optUploadFileRespDto = opcAliOssService
.uploadFile(file.getBytes(), fileName);
} catch (IOException e) {
e.printStackTrace();
logger.error("上传文件发生异常:{}", e.getMessage());
return WrapMapper.error("上传文件发生异常");
}
if (optUploadFileRespDto != null) {
optUploadFileRespDto.setFileType(fileType);
}
return WrapMapper.ok(optUploadFileRespDto);
}

问题没了

以上是关于spring cloud feign client 上传文件遇到问题记录的主要内容,如果未能解决你的问题,请参考以下文章

spring cloud 合约 - Feign Clients

spring cloud feign client 上传文件遇到问题记录

Spring Cloud Feign 组成和配置

Spring Cloud Feign Client 实现MultipartFile上传文件功能

带有 List 参数的 Spring Cloud Feign Client @RequestParam 创建错误的请求

声明式HTTP客户端 - Spring Cloud OpenFeign