refactor(uapbd): 优化材料下载功能
- 在 MaterialPlmDownloadAction 中添加日志记录,便于调试和监控 - 更新 GetPlmFileUtil 类中的文件下载逻辑,提高代码可读性和性能 - 修复 zip压缩包创建过程中的潜在资源泄漏问题
This commit is contained in:
parent
4bcbbe8cb0
commit
e53a7fa44e
|
@ -1,10 +1,13 @@
|
|||
package nccloud.web.uapbd.material.action;
|
||||
|
||||
import nc.bs.logging.Logger;
|
||||
import nc.bs.uapbd.util.GetPlmFileUtil;
|
||||
import nccloud.baseapp.core.log.NCCForUAPLogger;
|
||||
import nccloud.framework.core.io.WebFile;
|
||||
import nccloud.framework.web.action.itf.ICommonAction;
|
||||
import nccloud.framework.web.container.IRequest;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -21,11 +24,13 @@ public class MaterialPlmDownloadAction implements ICommonAction {
|
|||
try {
|
||||
Map<String, String[]> params_1 = request.readParameters();
|
||||
String[] pks = params_1.get("materialCode"); // »ñÈ¡ËùÓÐ pk
|
||||
String materialCode = "101092250323,101092250323";
|
||||
NCCForUAPLogger.debug("获取所有 pk:" + Arrays.toString(pks));
|
||||
String materialCode = "101092250323,101092250321,101092250322";
|
||||
String[] materialCodeArr = materialCode.split(",", -1);
|
||||
GetPlmFileUtil fileUtil = new GetPlmFileUtil();
|
||||
files = fileUtil.getPlmFiles(materialCodeArr);
|
||||
} catch (Exception e) {
|
||||
Logger.error("MaterialPlmDownloadAction-exp:" + e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return files;
|
||||
|
|
|
@ -88,8 +88,8 @@ public class GetPlmFileUtil {
|
|||
String objId = plmFileJson.getString("objId");
|
||||
String fname = plmFileJson.getString("fname");
|
||||
|
||||
byte[] pdfBytes = this.doDownloadPlmFile(objId);
|
||||
InputStream ins = new ByteArrayInputStream(pdfBytes);
|
||||
byte[] fileBytes = this.doDownloadPlmFile(objId);
|
||||
InputStream ins = new ByteArrayInputStream(fileBytes);
|
||||
|
||||
file = new WebFile(fname, ins);
|
||||
} else {
|
||||
|
@ -98,29 +98,14 @@ public class GetPlmFileUtil {
|
|||
ByteArrayOutputStream zipOut = new ByteArrayOutputStream();
|
||||
ZipOutputStream zipStream = new ZipOutputStream(zipOut);
|
||||
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+""));
|
||||
// i++;
|
||||
zipStream.write(bytes);
|
||||
byte[] fileBytes = this.doDownloadPlmFile(objId);
|
||||
zipStream.putNextEntry(new ZipEntry(fname));
|
||||
zipStream.write(fileBytes);
|
||||
zipStream.closeEntry();
|
||||
} finally {
|
||||
if (ins != null) {
|
||||
try {
|
||||
ins.close();
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to close input stream: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
zipStream.finish();
|
||||
} finally {
|
||||
|
@ -289,16 +274,6 @@ public class GetPlmFileUtil {
|
|||
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);
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue