From 865f7d0a7bdde7446e9a55b971a70141f812f436 Mon Sep 17 00:00:00 2001 From: mzr <1562242162@qq.com> Date: Sat, 2 Aug 2025 13:12:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96PLM=E5=9B=BE=E7=BA=B8?= =?UTF-8?q?=E7=9A=84=E5=B7=A5=E5=85=B7=E7=B1=BB-v0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nc/bs/uapbd/util/GetPlmFileUtil.java | 156 +++++++++++++++--- 1 file changed, 129 insertions(+), 27 deletions(-) diff --git a/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java b/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java index bc6787b..53be1cc 100644 --- a/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java +++ b/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java @@ -17,9 +17,18 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.Base64; +import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; /** * 获取PLM图纸的工具类 @@ -32,6 +41,7 @@ public class GetPlmFileUtil { private static final Log logger = Log.getInstance(LOG_INFO_NAME); private String plmBaseUrl = ""; private String plmUser = ""; + private String token = ""; private static final String tokenUrl = "/sipmweb/api/oauth"; // 根据物料编码获取零部件ID private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}?key={key}&start={start}&size={size}"; @@ -40,28 +50,57 @@ public class GetPlmFileUtil { // 下载文件 private String downlownUrl = "/sipmweb/web/download?rid={rid}&id={id}&t={t}&type={type}"; - public WebFile getPlmFile(String materialCode) { - try { - // 获取PLM的参数 - Map configParams = getConfigParams("Dldz-config"); - if (configParams == null || configParams.isEmpty()) { - throw new BusinessException("未配置PLM参数"); - } - plmBaseUrl = configParams.get("plmBaseUrl"); - plmUser = configParams.get("plmUser"); - // 获取token - String token = getToken(); - String materialId = getMaterialId(token, materialCode); - String fileId = getFileId(token, materialId); - downloadFile(token, fileId); - } catch (BusinessException | IOException e) { - logger.error("GetPlmFileUtil-getFile-exp:" + e.getMessage()); - throw new RuntimeException(e); - } + public WebFile getPlmFiles(String[] materialCodeArr) throws BusinessException, IOException { WebFile file = null; + if (materialCodeArr == null || materialCodeArr.length == 0) { + return file; + } + // 获取PLM的参数 + Map configParams = getConfigParams("Dldz-config"); + if (configParams == null || configParams.isEmpty()) { + throw new BusinessException("未配置PLM参数"); + } + plmBaseUrl = configParams.get("plmBaseUrl"); + plmUser = configParams.get("plmUser"); + token = getToken(); + if (materialCodeArr.length == 1) { + String materialCode = materialCodeArr[0]; + JSONObject plmFileJson = this.getPlmFile(materialCode); + String objId = plmFileJson.getString("objId"); + String fname = plmFileJson.getString("fname"); + InputStream ins = this.doDownloadPlmFile(objId); + file = new WebFile(fname, ins); + } else { + // 创建内存中的 ZIP 输出流 + ByteArrayOutputStream zipOut = new ByteArrayOutputStream(); + ZipOutputStream zipStream = new ZipOutputStream(zipOut); + for (String materialCode : materialCodeArr) { + JSONObject plmFileJson = this.getPlmFile(materialCode); + String objId = plmFileJson.getString("objId"); + String fname = plmFileJson.getString("fname"); + InputStream ins = this.doDownloadPlmFile(objId); + byte[] bytes = parseFileStream(ins); + zipStream.putNextEntry(new ZipEntry(fname)); + zipStream.write(bytes); + zipStream.closeEntry(); + } + zipStream.finish(); + zipStream.close(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); + String zipName = "物料图纸下载_" + sdf.format(new Date()); + // 构造 WebFile 返回 ZIP 文件 + InputStream ins = new ByteArrayInputStream(zipOut.toByteArray()); + file = new WebFile(zipName + ".zip", ins); + } + return file; } + public JSONObject getPlmFile(String materialCode) throws BusinessException, IOException { + String materialId = getMaterialId(materialCode); + return getFileId(materialId); + } + /** * 获取token @@ -83,7 +122,7 @@ public class GetPlmFileUtil { /** * 根据物料编码获取零部件ID */ - private String getMaterialId(String token, String materialCode) throws IOException, BusinessException { + private String getMaterialId(String materialCode) throws IOException, BusinessException { String fileUrl = plmBaseUrl + materialIdUrl; fileUrl = fileUrl.replace("{rid}", token); // 对象表名 MPART(零部件) @@ -105,7 +144,7 @@ public class GetPlmFileUtil { /** * 根据零部件ID获取文件ID */ - private String getFileId(String token, String materialId) throws IOException, BusinessException { + private JSONObject getFileId(String materialId) throws IOException, BusinessException { String fileUrl = plmBaseUrl + materialFileIdUrl; fileUrl = fileUrl.replace("{rid}", token); // 对象表名 MPART(零部件) @@ -121,25 +160,22 @@ public class GetPlmFileUtil { JSONObject jsonObject = JSONObject.parseObject(result); String objId = jsonObject.getString("objId"); if (objId == null || objId.isEmpty()) { - throw new BusinessException("获取PLM物料的文件id失败"); + throw new BusinessException("获取PLM物料的文件信息失败"); } - return objId; + return jsonObject; } /** * 调用PLM的下载文件接口 */ - private String downloadFile(String token, String fileId) throws IOException, BusinessException { + private InputStream doDownloadPlmFile(String fileId) throws IOException, BusinessException { String fileUrl = plmBaseUrl + downlownUrl; Map map = new HashMap<>(); map.put("rid", token); map.put("id", fileId);// 对象id map.put("t", "SIPM1");// 对象表名 map.put("type", "D");// 文件类别(BD,D)(D是文件本身的文件,BD是PDF图) - String result = doGet(fileUrl, map); - logger.error("GetPlmFileUtil-getFileId-result = " + result); - - return result; + return getFileFromPlm(fileUrl, map); } public Map getConfigParams(String code) { @@ -189,4 +225,70 @@ public class GetPlmFileUtil { get.releaseConnection(); return responseString; } + + /** + * 调用第三方文件接口并接收文件流 + * + * @param requestUrl 文件接口URL + * @return 文件流 + */ + private InputStream getFileFromPlm(String requestUrl, Map paramMap) throws IOException { + PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); + cm.setMaxTotal(500); + cm.setDefaultMaxPerRoute(50); + + RequestConfig globalConfig = RequestConfig.custom() + .setConnectionRequestTimeout(5000) + .setConnectTimeout(5000) + .setSocketTimeout(20000) + .setCookieSpec(CookieSpecs.IGNORE_COOKIES) + .build(); + + CloseableHttpClient httpClient = HttpClients.custom() + .setConnectionManager(cm) + .setDefaultRequestConfig(globalConfig) + .build(); + StringBuilder param = new StringBuilder("?"); + if (paramMap != null) { + for (Map.Entry entry : paramMap.entrySet()) { + param.append(entry.getKey()); + param.append("="); + param.append(entry.getValue()); + param.append("&"); + } + param.deleteCharAt(param.length() - 1); + } + String url = requestUrl + param; + HttpGet httpGet = new HttpGet(url); + + // 执行请求并返回文件流 + return httpClient.execute(httpGet, response -> { + // 检查响应状态 + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode >= 200 && statusCode < 300) { + return response.getEntity().getContent(); + } else { + throw new IOException("HTTP request failed with status code: " + statusCode); + } + }); + } + + /** + * 解析文件流 + * + * @param inputStream 文件流 + * @return 解析结果 + */ + private byte[] parseFileStream(InputStream inputStream) throws IOException { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[1024]; + while ((nRead = inputStream.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + byte[] byteArray = buffer.toByteArray(); + buffer.close(); + return byteArray; + } + }