对接金蝶上传附件接口

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

以下是核心功能代码

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);
		}
	}
相关推荐
考琪5 小时前
Nginx打印变量到log方法
java·运维·nginx
薛定谔的猫喵喵5 小时前
基于PyQt5的视频答题竞赛系统设计与实现
开发语言·qt·音视频
岱宗夫up5 小时前
Python 数据分析入门
开发语言·python·数据分析
wangjialelele5 小时前
Linux中的进程管理
java·linux·服务器·c语言·c++·个人开发
码界筑梦坊5 小时前
325-基于Python的校园卡消费行为数据可视化分析系统
开发语言·python·信息可视化·django·毕业设计
历程里程碑5 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
多恩Stone5 小时前
【RoPE】Flux 中的 Image Tokenization
开发语言·人工智能·python
李日灐5 小时前
C++进阶必备:红黑树从 0 到 1: 手撕底层,带你搞懂平衡二叉树的平衡逻辑与黑高检验
开发语言·数据结构·c++·后端·面试·红黑树·自平衡二叉搜索树
晔子yy5 小时前
如何设计让你的程序同时处理10w条数据
java
Risehuxyc5 小时前
备份三个PHP程序
android·开发语言·php