背景:因为前端调金蝶上传附件接口会报跨域,所以使用后端进行转发。
以下是核心功能代码
java
@Override
public R<Object> uploadAttachmentFile(MultipartFile file, AttachmentUploadFileArgsDTO attachmentUploadFileArgs) {
try {
// 1. 严格参数校验:文件非空+文件名非空+附件参数非空
AssertUtils.isTrue(!file.isEmpty(), "上传文件不能为空");
AssertUtils.isTrue(StrUtil.isNotBlank(file.getOriginalFilename()), "文件名称不能为空");
AssertUtils.notNull(attachmentUploadFileArgs, "附件业务参数不能为空");
log.info("开始转发文件到中台,文件名:{},业务参数:{}", file.getOriginalFilename(), JSON.toJSONString(attachmentUploadFileArgs));
// 2. 构建请求头:自动生成分隔符(boundary),禁止手动设置Content-Type
HttpHeaders headers = getHeaders(); // 继承中台基础头(accessToken、x-acgw-identity)
headers.setAccept(Collections.singletonList(MediaType.ALL)); // 接收所有响应类型
headers.setContentType(MediaType.MULTIPART_FORM_DATA); // 显式声明多表单类型(RestTemplate仍会自动加boundary)
// 3. 构建多表单请求体:文件+业务参数
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
// 3.1 添加上传业务参数:转为JSON字符串,适配中台参数格式
requestBody.add("attachmentUploadFileArgs", JSON.toJSONString(attachmentUploadFileArgs));
// 3.2 添加文件:使用ByteArrayResource包装,避免InputStream流关闭问题,处理文件名中文编码
String originalFilename = file.getOriginalFilename();
ByteArrayResource fileResource = new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return originalFilename; // 重写获取文件名,确保中台能正确解析
}
};
requestBody.add("file", fileResource);
// 4. 构建请求实体(请求体+请求头)
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
// 5. 拼接中台接口地址,发起POST请求
String uploadUrl = middlegroundProperties.getDomain() + attachmentUploadUri;
ResponseEntity<JSONObject> response = restTemplate.postForEntity(uploadUrl, requestEntity, JSONObject.class);
log.info("文件转发中台请求完成,URL:{},响应状态码:{}", uploadUrl, response.getStatusCodeValue());
if (!response.getStatusCode().is2xxSuccessful()) {
String errorMsg = String.format("文件转发中台失败,HTTP状态码:%d", response.getStatusCodeValue());
log.error(errorMsg);
return R.fail(errorMsg);
}
JSONObject responseBody = response.getBody();
AssertUtils.notNull(responseBody, "中台返回空响应,转发失败");
log.info("文件转发中台成功,中台原始响应:{}", responseBody.toJSONString());
// 透传中台完整响应(也可按需提取data字段:responseBody.getString("data"))
UploadAttachmentFileResultVO uploadAttachmentFileResultVO = JSON.parseObject(responseBody.toJSONString(), new TypeReference<UploadAttachmentFileResultVO>() {
});
return R.data(uploadAttachmentFileResultVO);
} catch (IOException e) {
// 文件读取异常:如文件损坏、流读取失败
String errorMsg = "读取上传文件失败,原因:" + e.getMessage();
log.error(errorMsg, e);
return R.fail(errorMsg);
} catch (IllegalArgumentException e) {
// 参数校验异常:如文件空、参数空
log.warn("文件上传参数校验失败:{}", e.getMessage());
return R.fail(e.getMessage());
} catch (Exception e) {
// 全局异常:如中台接口不可达、网络异常、序列化失败
String errorMsg = "文件转发中台异常,系统内部错误:" + e.getMessage();
log.error(errorMsg, e);
return R.fail(errorMsg);
}
}