From 6ff1a33621990ce5ce460300d9a278c94eb679e9 Mon Sep 17 00:00:00 2001 From: lihao Date: Tue, 5 Aug 2025 16:17:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=BA=B8=E4=B8=8B=E8=BD=BD=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nc/bs/uapbd/util/GetPlmFileUtil.java | 70 +++++++++++-------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java b/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java index 47bf267..98d520d 100644 --- a/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java +++ b/uapbd/src/public/nc/bs/uapbd/util/GetPlmFileUtil.java @@ -17,6 +17,7 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; +import javax.xml.bind.DatatypeConverter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -86,7 +87,10 @@ public class GetPlmFileUtil { JSONObject plmFileJson = this.getPlmFile(materialCode); String objId = plmFileJson.getString("objId"); String fname = plmFileJson.getString("fname"); - InputStream ins = this.doDownloadPlmFile(objId); + + byte[] pdfBytes = this.doDownloadPlmFile(objId); + InputStream ins = new ByteArrayInputStream(pdfBytes); + file = new WebFile(fname, ins); } else { // 多个物料合并成zip文件输出文件流 @@ -94,29 +98,33 @@ public class GetPlmFileUtil { ByteArrayOutputStream zipOut = new ByteArrayOutputStream(); ZipOutputStream zipStream = new ZipOutputStream(zipOut); try { - for (String materialCode : materialCodeArr) { - JSONObject plmFileJson = this.getPlmFile(materialCode); - String objId = plmFileJson.getString("objId"); - String fname = plmFileJson.getString("fname"); - InputStream ins = this.doDownloadPlmFile(objId); - try { +// int i=0; + for (String materialCode : materialCodeArr) { + JSONObject plmFileJson = this.getPlmFile(materialCode); + String objId = plmFileJson.getString("objId"); + String fname = plmFileJson.getString("fname"); + byte[] pdfBytes =this.doDownloadPlmFile(objId); + InputStream ins = new ByteArrayInputStream(pdfBytes); +// InputStream ins = this.doDownloadPlmFile(objId); + try { byte[] bytes = parseFileStream(ins); - zipStream.putNextEntry(new ZipEntry(fname)); - zipStream.write(bytes); - zipStream.closeEntry(); + zipStream.putNextEntry(new ZipEntry(fname+"")); +// i++; + zipStream.write(bytes); + zipStream.closeEntry(); } finally { if (ins != null) { try { ins.close(); - } catch (IOException e) { - logger.error("Failed to close input stream: " + e.getMessage()); - } - } + } catch (IOException e) { + logger.error("Failed to close input stream: " + e.getMessage()); + } + } } } - zipStream.finish(); + zipStream.finish(); } finally { - zipStream.close(); + zipStream.close(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String zipName = "物料图纸下载_" + sdf.format(new Date()); @@ -212,7 +220,7 @@ public class GetPlmFileUtil { /** * 调用PLM的下载文件接口 */ - private InputStream doDownloadPlmFile(String fileId) throws IOException { + private byte[] doDownloadPlmFile(String fileId) throws IOException { String fileUrl = plmBaseUrl + downlownUrl; Map map = new HashMap<>(); map.put("rid", token); @@ -265,7 +273,7 @@ public class GetPlmFileUtil { * @param requestUrl 文件接口URL * @return 文件流 */ - private InputStream getFileFromPlm(String requestUrl, Map paramMap) throws IOException { + private byte[] getFileFromPlm(String requestUrl, Map paramMap) throws IOException { StringBuilder param = new StringBuilder("?"); if (paramMap != null) { for (Map.Entry entry : paramMap.entrySet()) { @@ -277,18 +285,20 @@ public class GetPlmFileUtil { 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); - } - }); + HttpGet get = new HttpGet(url); + byte[] responseString = httpClient.execute(get, response -> EntityUtils.toByteArray(response.getEntity())); + get.releaseConnection(); + return responseString; +// // 执行请求并返回文件流 +// 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); +// } +// }); } /**