获取PLM图纸的工具类-v0

This commit is contained in:
mzr 2025-08-02 13:12:50 +08:00
parent 976c693b6d
commit 865f7d0a7b
1 changed files with 129 additions and 27 deletions

View File

@ -17,9 +17,18 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; 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.HashMap;
import java.util.Map; import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/** /**
* 获取PLM图纸的工具类 * 获取PLM图纸的工具类
@ -32,6 +41,7 @@ public class GetPlmFileUtil {
private static final Log logger = Log.getInstance(LOG_INFO_NAME); private static final Log logger = Log.getInstance(LOG_INFO_NAME);
private String plmBaseUrl = ""; private String plmBaseUrl = "";
private String plmUser = ""; private String plmUser = "";
private String token = "";
private static final String tokenUrl = "/sipmweb/api/oauth"; private static final String tokenUrl = "/sipmweb/api/oauth";
// 根据物料编码获取零部件ID // 根据物料编码获取零部件ID
private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}?key={key}&start={start}&size={size}"; private String materialIdUrl = "/sipmweb/api/{rid}/search/{t}?key={key}&start={start}&size={size}";
@ -40,8 +50,11 @@ public class GetPlmFileUtil {
// 下载文件 // 下载文件
private String downlownUrl = "/sipmweb/web/download?rid={rid}&id={id}&t={t}&type={type}"; private String downlownUrl = "/sipmweb/web/download?rid={rid}&id={id}&t={t}&type={type}";
public WebFile getPlmFile(String materialCode) { public WebFile getPlmFiles(String[] materialCodeArr) throws BusinessException, IOException {
try { WebFile file = null;
if (materialCodeArr == null || materialCodeArr.length == 0) {
return file;
}
// 获取PLM的参数 // 获取PLM的参数
Map<String, String> configParams = getConfigParams("Dldz-config"); Map<String, String> configParams = getConfigParams("Dldz-config");
if (configParams == null || configParams.isEmpty()) { if (configParams == null || configParams.isEmpty()) {
@ -49,19 +62,45 @@ public class GetPlmFileUtil {
} }
plmBaseUrl = configParams.get("plmBaseUrl"); plmBaseUrl = configParams.get("plmBaseUrl");
plmUser = configParams.get("plmUser"); plmUser = configParams.get("plmUser");
// 获取token token = getToken();
String token = getToken(); if (materialCodeArr.length == 1) {
String materialId = getMaterialId(token, materialCode); String materialCode = materialCodeArr[0];
String fileId = getFileId(token, materialId); JSONObject plmFileJson = this.getPlmFile(materialCode);
downloadFile(token, fileId); String objId = plmFileJson.getString("objId");
} catch (BusinessException | IOException e) { String fname = plmFileJson.getString("fname");
logger.error("GetPlmFileUtil-getFile-exp:" + e.getMessage()); InputStream ins = this.doDownloadPlmFile(objId);
throw new RuntimeException(e); 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();
} }
WebFile file = null; 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; return file;
} }
public JSONObject getPlmFile(String materialCode) throws BusinessException, IOException {
String materialId = getMaterialId(materialCode);
return getFileId(materialId);
}
/** /**
* 获取token * 获取token
@ -83,7 +122,7 @@ public class GetPlmFileUtil {
/** /**
* 根据物料编码获取零部件ID * 根据物料编码获取零部件ID
*/ */
private String getMaterialId(String token, String materialCode) throws IOException, BusinessException { private String getMaterialId(String materialCode) throws IOException, BusinessException {
String fileUrl = plmBaseUrl + materialIdUrl; String fileUrl = plmBaseUrl + materialIdUrl;
fileUrl = fileUrl.replace("{rid}", token); fileUrl = fileUrl.replace("{rid}", token);
// 对象表名 MPART零部件 // 对象表名 MPART零部件
@ -105,7 +144,7 @@ public class GetPlmFileUtil {
/** /**
* 根据零部件ID获取文件ID * 根据零部件ID获取文件ID
*/ */
private String getFileId(String token, String materialId) throws IOException, BusinessException { private JSONObject getFileId(String materialId) throws IOException, BusinessException {
String fileUrl = plmBaseUrl + materialFileIdUrl; String fileUrl = plmBaseUrl + materialFileIdUrl;
fileUrl = fileUrl.replace("{rid}", token); fileUrl = fileUrl.replace("{rid}", token);
// 对象表名 MPART零部件 // 对象表名 MPART零部件
@ -121,25 +160,22 @@ public class GetPlmFileUtil {
JSONObject jsonObject = JSONObject.parseObject(result); JSONObject jsonObject = JSONObject.parseObject(result);
String objId = jsonObject.getString("objId"); String objId = jsonObject.getString("objId");
if (objId == null || objId.isEmpty()) { if (objId == null || objId.isEmpty()) {
throw new BusinessException("获取PLM物料的文件id失败"); throw new BusinessException("获取PLM物料的文件信息失败");
} }
return objId; return jsonObject;
} }
/** /**
* 调用PLM的下载文件接口 * 调用PLM的下载文件接口
*/ */
private String downloadFile(String token, String fileId) throws IOException, BusinessException { private InputStream doDownloadPlmFile(String fileId) throws IOException, BusinessException {
String fileUrl = plmBaseUrl + downlownUrl; String fileUrl = plmBaseUrl + downlownUrl;
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("rid", token); map.put("rid", token);
map.put("id", fileId);// 对象id map.put("id", fileId);// 对象id
map.put("t", "SIPM1");// 对象表名 map.put("t", "SIPM1");// 对象表名
map.put("type", "D");// 文件类别BD,DD是文件本身的文件BD是PDF图 map.put("type", "D");// 文件类别BD,DD是文件本身的文件BD是PDF图
String result = doGet(fileUrl, map); return getFileFromPlm(fileUrl, map);
logger.error("GetPlmFileUtil-getFileId-result = " + result);
return result;
} }
public Map<String, String> getConfigParams(String code) { public Map<String, String> getConfigParams(String code) {
@ -189,4 +225,70 @@ public class GetPlmFileUtil {
get.releaseConnection(); get.releaseConnection();
return responseString; return responseString;
} }
/**
* 调用第三方文件接口并接收文件流
*
* @param requestUrl 文件接口URL
* @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()) {
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;
}
} }