码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-01-17 21:01:45      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:index   control   last   json   ted   auth   ack   记录   pca   

项目中用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 上传文件遇到问题记录

标签:index   control   last   json   ted   auth   ack   记录   pca   

原文地址:https://www.cnblogs.com/zjhgx/p/12207387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!