对接金蝶上传附件接口

背景:因为前端调金蝶上传附件接口会报跨域,所以使用后端进行转发。

以下是核心功能代码

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);
		}
	}
相关推荐
1024奇点8 小时前
【仓颉语言入门 · 第二篇 第 22 课】
开发语言·ide·开源
Warren2Lynch9 小时前
C4 模型详解:利用 Visual Paradigm 可视化软件架构的综合指南
开发语言·uml
天河归来9 小时前
第三方接口不稳定,如何让核心任务继续完成
java·大数据
Knight_AL10 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查
java·开发语言
FRing0310 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2
by2099911 小时前
C++ 智能指针的所有权与使用
开发语言·c++·经验分享
砚底藏山河11 小时前
python量化自定义周期 K 线合成
java·开发语言·数据库·python·金融
IT召唤狮11 小时前
开源企业级零信任项目Pangolin,零信任能挡住PHP核弹漏洞吗?
开发语言·开源·php
知已难寻11 小时前
03 · OpenStack的计算管理 Nova
运维·服务器·开发语言·云原生·云计算·openstack·开源软件
Rover Ramble11 小时前
C++ 用互斥锁实现读写锁
开发语言·c++