优化同时下载文件和图纸

This commit is contained in:
lihao 2025-09-15 21:20:49 +08:00
parent d60e215f02
commit 5d68e40f57
1 changed files with 49 additions and 106 deletions

View File

@ -33,7 +33,7 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 获取物料PLM文件
* 获取物料PLM文件同时下载SIPM1和SIPM2
*
* @author mzr
* @date 2025/8/4
@ -47,7 +47,7 @@ public class MaterialPlmDownloadAction implements ICommonAction {
private static final String tokenUrl = "/sipmweb/api/oauth";
// 根据物料编码获取零部件ID
private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}";
// 根据零部件ID获取二维图档ID及信息
// 根据零部件ID获取图档ID及信息
private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data";
// 下载文件
private String downlownUrl = "/sipmweb/web/download";
@ -61,12 +61,9 @@ public class MaterialPlmDownloadAction implements ICommonAction {
if (materialCodeArr == null || materialCodeArr.length == 0) {
ExceptionUtils.wrapBusinessException("物料编码不能为空");
}
// NCCForUAPLogger.debug("MaterialPlmDownloadAction-pk = " + Arrays.toString(materialCodeArr));
// String materialCode = "101092250323,101092250321,101092250322";
// String[] materialCodeArr = materialCode.split(",", -1);
files = this.getPlmFiles(materialCodeArr);
} catch (Exception ex) {
logger.error("MaterialPlmDownloadAction-exp:" + ex.getMessage());
logger.error("MaterialPlmDownloadAction-exp:" + ex.getMessage(), ex);
// 将异常转换为WebFile的形式返回以便前端展示报错
NCCForPrintLogger.error(ex);
Throwable e = org.apache.commons.lang3.exception.ExceptionUtils.getRootCause(ex);
@ -87,9 +84,8 @@ public class MaterialPlmDownloadAction implements ICommonAction {
}
public WebFile getPlmFiles(String[] materialCodeArr) throws Exception {
WebFile file = null;
if (materialCodeArr == null || materialCodeArr.length == 0) {
return file;
return null;
}
// 获取PLM的参数
Map<String, String> configParams = getConfigParams("Dldz-config");
@ -99,45 +95,13 @@ public class MaterialPlmDownloadAction implements ICommonAction {
plmBaseUrl = configParams.get("plmBaseUrl");
plmUser = configParams.get("plmUser");
token = getToken();
if (materialCodeArr.length == 1) {
String materialCode = materialCodeArr[0];
String materialId = getMaterialId(materialCode);
if (StringUtils.isEmpty(materialId)) {
throw new BusinessException("物料编码:" + materialCode + ",获取PLM物料id失败,请检查PLM是否存在对应的物料");
}
String fileRes = this.getFileId(materialId);
JSONObject fileJson = JSONObject.parseObject(fileRes);
String list = fileJson.getString("list");
JSONArray jsonArray = JSONObject.parseArray(list);
if (jsonArray == null || jsonArray.isEmpty()) {
throw new BusinessException("物料编码:" + materialCode + ",获取PLM物料的文件信息失败");
}
JSONObject plmFileJson = jsonArray.getJSONObject(0);
String objId = plmFileJson.getString("objId");
if (objId == null || objId.isEmpty()) {
throw new BusinessException("物料编码:" + materialCode + ",获取PLM物料的文件信息失败");
}
String fname = plmFileJson.getString("fname");
byte[] fileBytes = this.doDownloadPlmFile(objId);
if (fileBytes.length == 0) {
throw new BusinessException("物料编码:" + materialCode + ",未查询到PLM文件");
}
InputStream ins = new ByteArrayInputStream(fileBytes);
file = new WebFile(fname, ins);
} else {
file = processMultipleFiles(materialCodeArr);
}
return file;
// 无论单个还是多个物料均打包为ZIP原单个物料返回单文件无法同时返回两类文件
return processMultipleFiles(materialCodeArr);
}
/**
* 处理多个文件并打包
*
* @param materialCodeArr 物料编码数组
* @return 打包后的zip文件WebFile对象
* @throws Exception 异常
* 处理多个文件并打包同时包含SIPM1和SIPM2
*/
private WebFile processMultipleFiles(String[] materialCodeArr) throws Exception {
// 在内存中的 ZIP 输出流
@ -148,12 +112,13 @@ public class MaterialPlmDownloadAction implements ICommonAction {
for (int i = 0; i < materialCodeArr.length; i++) {
String materialCode = materialCodeArr[i];
try {
boolean success = processSingleMaterialToZip(materialCode, i, zipStream);
if (success) {
successCount++;
}
// 同时处理当前物料的SIPM1和SIPM2
boolean success1 = processSingleMaterialToZip(materialCode, i, "SIPM1", zipStream);
boolean success2 = processSingleMaterialToZip(materialCode, i, "SIPM2", zipStream);
if (success1) successCount++;
if (success2) successCount++;
} catch (BusinessException e) {
logger.error("处理物料编码 " + materialCode + " 时出现异常: " + e.getMessage());
logger.error("处理物料编码 " + materialCode + " 时出现异常: " + e.getMessage(), e);
// 继续处理下一个
}
}
@ -162,58 +127,59 @@ public class MaterialPlmDownloadAction implements ICommonAction {
zipStream.close();
}
// 如果没有任何文件成功处理
if (successCount == 0) {
throw new BusinessException("没有有效的文件可以打包");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String zipName = "图纸文档打包_" + sdf.format(new Date());
// 创建 WebFile 对象用于 ZIP 文件
InputStream ins = new ByteArrayInputStream(zipOut.toByteArray());
return new WebFile(zipName + ".zip", ins);
}
/**
* 将单个物料文件添加到ZIP流中
*
* @param materialCode 物料编码
* @param index 索引
* @param zipStream ZIP输出流
* @return boolean 是否成功处理
* @throws Exception 异常
* 将单个物料的单个类型文件SIPM1/SIPM2添加到ZIP流中
*/
private boolean processSingleMaterialToZip(String materialCode, int index, ZipOutputStream zipStream) throws Exception {
private boolean processSingleMaterialToZip(String materialCode, int index, String fileType, ZipOutputStream zipStream) throws Exception {
String materialId = getMaterialId(materialCode);
if (StringUtils.isEmpty(materialId)) {
logger.warn("物料编码 " + materialCode + " 未查询到PLM物料ID");
return false;
}
String fileRes = this.getFileId(materialId);
// 根据文件类型获取对应的文件信息复用原有getFileId逻辑通过参数区分类型
String fileRes = getFileIdByType(materialId, fileType);
JSONObject fileJson = JSONObject.parseObject(fileRes);
String list = fileJson.getString("list");
JSONArray jsonArray = JSONObject.parseArray(list);
if (jsonArray == null || jsonArray.isEmpty()) {
logger.warn("物料编码 " + materialCode + " 未查询到" + fileType + "类型文件");
return false;
}
JSONObject plmFileJson = jsonArray.getJSONObject(0);
String objId = plmFileJson.getString("objId");
if (objId == null || objId.isEmpty()) {
logger.warn("物料编码 " + materialCode + "" + fileType + "文件ID为空");
return false;
}
String name = plmFileJson.getString("name");
String suffix = plmFileJson.getString("suffix");
byte[] fileBytes = this.doDownloadPlmFile(objId);
byte[] fileBytes = downloadPlmFileByType(objId, fileType);
if (fileBytes.length == 0) {
logger.warn("物料编码 " + materialCode + "" + fileType + "文件字节流为空");
return false;
}
String fname = name + "_" + materialCode + "_" + index + "." + suffix;
// 生成带类型标识的文件名避免重复
String fname = name + "_" + materialCode + "_" + fileType + "_" + index + "." + suffix;
zipStream.putNextEntry(new ZipEntry(fname));
zipStream.write(fileBytes);
zipStream.closeEntry();
return true;
}
/**
* 获取token
*/
@ -222,7 +188,6 @@ public class MaterialPlmDownloadAction implements ICommonAction {
tokenMap.put("uname", plmUser);
tokenMap.put("f", "true");
String tokenStr = doGet(plmBaseUrl + tokenUrl, tokenMap);
// logger.error("GetPlmFileUtil-getToken-tokenStr = " + tokenStr);
JSONObject jsonObject = JSONObject.parseObject(tokenStr);
String token = jsonObject.getString("errmsg");
if (token == null || token.isEmpty()) {
@ -237,12 +202,11 @@ public class MaterialPlmDownloadAction implements ICommonAction {
private String getMaterialId(String materialCode) throws IOException, BusinessException {
String fileUrl = plmBaseUrl + materialIdUrl;
fileUrl = fileUrl.replace("{rid}", token);
// 对象表名 MPART零部件
fileUrl = fileUrl.replace("{t}", "MPART");
Map<String, String> map = new HashMap<>();
map.put("key", materialCode);// 搜索关键字
map.put("start", "0");// 偏移量
map.put("size", "10");// 数量
map.put("key", materialCode);
map.put("start", "0");
map.put("size", "10");
String result = doGet(fileUrl, map);
logger.error("GetPlmFileUtil-getMaterialId-result = " + result);
JSONObject jsonObject = JSONObject.parseObject(result);
@ -252,42 +216,36 @@ public class MaterialPlmDownloadAction implements ICommonAction {
return "";
}
JSONObject listJson = jsonArray.getJSONObject(0);
String objId = listJson.getString("objId");
if (objId == null || objId.isEmpty()) {
return "";
}
return objId;
return listJson.getString("objId");
}
/**
* 根据零部件ID获取文件ID
* 通用方法根据类型获取文件ID合并原getFileId和getFileId2
*/
private String getFileId(String materialId) throws IOException, BusinessException {
private String getFileIdByType(String materialId, String fileType) throws IOException, BusinessException {
String fileUrl = plmBaseUrl + materialFileIdUrl;
fileUrl = fileUrl.replace("{rid}", token);
// 对象表名 MPART零部件
fileUrl = fileUrl.replace("{t}", "MPART");
// 对象id
fileUrl = fileUrl.replace("{id}", materialId);
Map<String, String> map = new HashMap<>();
map.put("re", "MPART_SIPM1");// 关系ID 二维图档MPART_SIPM1
map.put("start", "0");// 偏移量
map.put("item", "SIPM1");// 关联对象表名
map.put("re", "MPART_" + fileType); // 动态生成关系ID
map.put("start", "0");
map.put("item", fileType); // 动态传入文件类型
String result = doGet(fileUrl, map);
logger.error("GetPlmFileUtil-getFileId-result = " + result);
logger.error("GetPlmFileUtil-getFileId-" + fileType + "-result = " + result);
return result;
}
/**
* 调用PLM的下载文件接口
* 通用方法根据类型下载文件合并原doDownloadPlmFile和doDownloadPlmFile2
*/
private byte[] doDownloadPlmFile(String fileId) throws IOException {
private byte[] downloadPlmFileByType(String fileId, String fileType) throws IOException {
String fileUrl = plmBaseUrl + downlownUrl;
Map<String, String> map = new HashMap<>();
map.put("rid", token);
map.put("id", fileId);// 对象id
map.put("t", "SIPM1");// 对象表名
map.put("type", "D");// 文件类别BD,DD是文件本身的文件BD是PDF图
map.put("id", fileId);
map.put("t", fileType); // 动态传入文件类型
map.put("type", "D");
return getFileFromPlm(fileUrl, map);
}
@ -309,15 +267,14 @@ public class MaterialPlmDownloadAction implements ICommonAction {
return map;
}
private String doGet(String requestUrl, Map<String, String> paramMap) throws IOException {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(5000);
cm.setDefaultMaxPerRoute(500);
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000) // 连接池获取连接超时
.setConnectTimeout(50000) // 连接建立超时
.setSocketTimeout(200000) // 等待响应超时
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000)
.setConnectTimeout(50000)
.setSocketTimeout(200000)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
@ -329,20 +286,14 @@ public class MaterialPlmDownloadAction implements ICommonAction {
return responseString;
}
/**
* 调用第三方文件接口并接收文件流
*
* @param requestUrl 文件接口URL
* @return 文件流
*/
private byte[] getFileFromPlm(String requestUrl, Map<String, String> paramMap) throws IOException {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(5000);
cm.setDefaultMaxPerRoute(500);
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000) // 连接池获取连接超时
.setConnectTimeout(50000) // 连接建立超时
.setSocketTimeout(200000) // 等待响应超时
RequestConfig globalConfig = RequestConfig.custom().setConnectionRequestTimeout(50000)
.setConnectTimeout(50000)
.setSocketTimeout(200000)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
@ -354,13 +305,6 @@ public class MaterialPlmDownloadAction implements ICommonAction {
return responseString;
}
/**
* 构建带参数的URL
*
* @param baseUrl 基础URL
* @param paramMap 参数Map
* @return 完整URL
*/
private String buildUrlWithParams(String baseUrl, Map<String, String> paramMap) {
StringBuilder param = new StringBuilder("?");
if (paramMap != null && !paramMap.isEmpty()) {
@ -374,5 +318,4 @@ public class MaterialPlmDownloadAction implements ICommonAction {
}
return baseUrl + param;
}
}