项目中用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 待修改,增加loginAuthDtoresponse = 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();-------------
@Overridepublic 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);}
问题没了
原文地址:https://www.cnblogs.com/zjhgx/p/12207387.html
时间: 2024-11-05 16:03:25