对接金蝶上传附件接口

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

以下是核心功能代码

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);
		}
	}
相关推荐
C+++Python6 小时前
C++ 进阶学习完整指南
java·c++·学习
sparEE6 小时前
c++值类别、右值引用和移动语义
开发语言·c++
zhangjw346 小时前
第11篇:Java Map集合详解,HashMap底层原理、哈希冲突、JDK1.8优化、遍历方式彻底吃透
java·开发语言·哈希算法
还得是你大哥7 小时前
Java互联网医院管理系统源码SpringBoot
java·spring boot·vue
阿维的博客日记7 小时前
怎么用ThreadLocal解决用户的登录上下文
java·juc
Dicky-_-zhang8 小时前
容器编排实战:Docker与Kubernetes对比选型与落地实践
java·jvm
benpaodeDD8 小时前
视频10,11,12,13——java程序的加载与执行,安装jdk
java·开发语言
一颗牙牙8 小时前
安装mmcv
开发语言·python·深度学习
大空大地20268 小时前
C#高级语法总结
开发语言·c#