refactor(uapbd): 重构 GetPlmFileUtil 类

- 移除了未使用的导入语句
- 新增了静态的 HttpClient 实例,提高了性能
-优化了多个物料文件下载并合并成 zip 文件的逻辑
- 添加了资源关闭的异常处理,避免了资源泄露
- 优化了错误日志的输出,提高了错误信息的可读性
This commit is contained in:
mzr 2025-08-04 08:19:31 +08:00
parent b210ccafd0
commit 782db385d8
1 changed files with 49 additions and 44 deletions

View File

@ -3,7 +3,6 @@ package nc.bs.uapbd.util;
import com.alibaba.fastjson.JSONObject;
import nc.bs.dao.DAOException;
import nc.bs.logging.Log;
import nc.bs.logging.Logger;
import nc.bs.trade.business.HYSuperDMO;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.cmp.util.StringUtils;
@ -21,9 +20,7 @@ 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;
@ -49,8 +46,28 @@ public class GetPlmFileUtil {
private String materialFileIdUrl = "/sipmweb/api/{rid}/relation/{t}/{id}/data?re={re}&start={start}&item={item}";
// 下载文件
private String downlownUrl = "/sipmweb/web/download?rid={rid}&id={id}&t={t}&type={type}";
// http请求
private static final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
private static final CloseableHttpClient httpClient;
public WebFile getPlmFiles(String[] materialCodeArr) throws BusinessException, IOException {
static {
connectionManager.setMaxTotal(5000);
connectionManager.setDefaultMaxPerRoute(500);
RequestConfig globalConfig = RequestConfig.custom()
.setConnectionRequestTimeout(50000)
.setConnectTimeout(50000)
.setSocketTimeout(200000)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.build();
httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(globalConfig)
.build();
}
public WebFile getPlmFiles(String[] materialCodeArr) throws Exception {
WebFile file = null;
if (materialCodeArr == null || materialCodeArr.length == 0) {
return file;
@ -71,21 +88,35 @@ public class GetPlmFileUtil {
InputStream ins = this.doDownloadPlmFile(objId);
file = new WebFile(fname, ins);
} else {
// 多个物料合并成zip文件输出文件流
// 创建内存中的 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();
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 {
byte[] bytes = parseFileStream(ins);
zipStream.putNextEntry(new ZipEntry(fname));
zipStream.write(bytes);
zipStream.closeEntry();
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
logger.error("Failed to close input stream: " + e.getMessage());
}
}
}
}
zipStream.finish();
} finally {
zipStream.close();
}
zipStream.finish();
zipStream.close();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String zipName = "物料图纸下载_" + sdf.format(new Date());
// 构造 WebFile 返回 ZIP 文件
@ -96,7 +127,7 @@ public class GetPlmFileUtil {
return file;
}
public JSONObject getPlmFile(String materialCode) throws BusinessException, IOException {
public JSONObject getPlmFile(String materialCode) throws Exception {
String materialId = getMaterialId(materialCode);
return getFileId(materialId);
}
@ -168,7 +199,7 @@ public class GetPlmFileUtil {
/**
* 调用PLM的下载文件接口
*/
private InputStream doDownloadPlmFile(String fileId) throws IOException, BusinessException {
private InputStream doDownloadPlmFile(String fileId) throws IOException {
String fileUrl = plmBaseUrl + downlownUrl;
Map<String, String> map = new HashMap<>();
map.put("rid", token);
@ -191,24 +222,13 @@ public class GetPlmFileUtil {
}
}
} catch (DAOException e) {
Logger.error("getConfigParams-exp:" + e.getMessage());
logger.error("Failed to get config parameters for code: " + code, e);
}
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) // 等待响应超时
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm)
.setDefaultRequestConfig(globalConfig).build();
StringBuilder param = new StringBuilder("?");
if (paramMap != null) {
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
@ -233,21 +253,6 @@ public class GetPlmFileUtil {
* @return 文件流
*/
private InputStream getFileFromPlm(String requestUrl, Map<String, String> 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<String, String> entry : paramMap.entrySet()) {