From 72e6bac02526caebc3797a7c2934d12896805708 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期二, 23 九月 2025 00:47:18 +0800
Subject: [PATCH] 修复文件状态读取问题
---
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java | 64 ++++++++++++++++++++++++-------
1 files changed, 49 insertions(+), 15 deletions(-)
diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
index f5ac1a9..5c2dc5c 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
@@ -16,6 +16,7 @@
import org.springblade.mdm.basesetting.machine.service.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.commons.contants.RegExpConstants;
+import org.springblade.mdm.commons.contants.ZipConstants;
import org.springblade.mdm.gkw.programnode.vo.ProgramNameVO;
import org.springblade.mdm.machinefile.entity.FileSendRecord;
import org.springblade.mdm.machinefile.service.FileSendRecordService;
@@ -30,6 +31,8 @@
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -85,14 +88,8 @@
Path tempZipFile = Files.createTempFile("mdmimpfile-"+System.currentTimeMillis(), ".zip");
file.transferTo(tempZipFile);
extractZipToTempDir(tempZipFile,extractDir);
-
-
}else{
//鏅�氭枃浠讹紝鐩存帴鏀惧叆extract鏂囦欢澶�
- String basename = FilenameUtils.getBaseName(file.getOriginalFilename());
- String ext = FilenameUtils.getExtension(file.getOriginalFilename());
- //Path filepath = Files.createTempFile(extractDir,basename,StringUtils.isEmpty(ext) ? "": "."+ext);
- //Path filepath = Files.createFile();
file.transferTo(Paths.get(extractDir.toString()+File.separator+file.getOriginalFilename()));
}
@@ -111,13 +108,24 @@
* @param extractDir 鐩爣鐩綍
* @throws IOException 鏂囦欢鎿嶄綔寮傚父
*/
- public void extractZipToTempDir(Path zipFilePath,Path extractDir) throws IOException {
+ public void extractZipToTempDir(Path zipFilePath,Path extractDir) throws IOException{
+ for (String encoding : ZipConstants.TRY_ENCODINGS) {
+ try {
+ extractZipToTempDirWithCharset(zipFilePath,extractDir,Charset.forName(encoding));
+ log.error("浣跨敤缂栫爜 {} 瑙f瀽鎴愬姛 ",encoding);
+ break;
+ } catch (Exception e) {
+ log.error("浣跨敤缂栫爜 {} 瑙f瀽澶辫触: ",encoding,e);
+ }
+ }
+ }
+ public void extractZipToTempDirWithCharset(Path zipFilePath, Path extractDir, Charset charset) throws IOException {
// 鑾峰彇绯荤粺涓存椂鐩綍
String tempDir = System.getProperty("java.io.tmpdir");
// 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
try (InputStream fis = Files.newInputStream(zipFilePath);
- ZipInputStream zis = new ZipInputStream(fis)) {
+ ZipInputStream zis = new ZipInputStream(fis,charset)) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
@@ -177,8 +185,7 @@
try (Stream<Path> paths = Files.walk(extractDir)) {
List<Path> filePathList = paths
.filter(Files::isRegularFile).toList(); // 鍙繚鐣欐櫘閫氭枃浠讹紝鎺掗櫎鐩綍
- //.collect(Collectors.toList());
- //System.out.println("鎵�鏈夋枃浠�"+filePathList);
+
for(Path path : filePathList){
list.add(readFileToVO(path));
}
@@ -205,19 +212,47 @@
}*/
return list;
}
+ public static byte[] getUTF8BytesFromGBKString(String gbkStr) {
+ int n = gbkStr.length();
+ byte[] utfBytes = new byte[3 * n];
+ int k = 0;
+ for (int i = 0; i < n; i++) {
+ int m = gbkStr.charAt(i);
+ if (m < 128 && m >= 0) {
+ utfBytes[k++] = (byte) m;
+ continue;
+ }
+ utfBytes[k++] = (byte) (0xe0 | (m >> 12));
+ utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
+ utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
+ }
+ if (k < utfBytes.length) {
+ byte[] tmp = new byte[k];
+ System.arraycopy(utfBytes, 0, tmp, 0, k);
+ return tmp;
+ }
+ return utfBytes;
+ }
+ ProgramNameVO tryParseProgramName(String fiilename){
+ ProgramNameVO pnmameVO = ProgramFileNameParser.parseProgramName(fiilename);//鏍囧噯utf8缂栫爜
+ if(pnmameVO.getDrawingNo() == null) {
+ //浣跨敤GBK缂栫爜瑙f瀽
+ pnmameVO = ProgramFileNameParser.parseProgramName(new String(getUTF8BytesFromGBKString(fiilename), StandardCharsets.UTF_8));
+ }
+ return pnmameVO;
+ }
/**
* 灏嗘枃浠剁粍缁囨垚VO
* @param path 鏂囦欢path
* @return vo
*/
- MdmProgramImportVO readFileToVO(Path path){
+ MdmProgramImportVO readFileToVO(Path path) throws UnsupportedEncodingException {
MdmProgramImportVO vo = new MdmProgramImportVO();
vo.setFilename(path.getFileName().toString());
- ProgramNameVO pnmameVO = ProgramFileNameParser.parseProgramName(vo.getFilename());
+ ProgramNameVO pnmameVO = tryParseProgramName(vo.getFilename());
vo.setDrawingNo(pnmameVO.getDrawingNo());
- //vo.setDrawingNo(parseDrawingNo(vo.getFilename()));
try (InputStream inputStream = Files.newInputStream(path)) {
// 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
@@ -234,7 +269,7 @@
AnnotationProperties defAnnoProperties = AnnotationProperties.getDefault();
String sendPathLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getSendPathLineIndex());
- //bas.mark(0);
+
bas.reset();
String statusLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getStatusLineIndex());
log.info("sendPathLine={}", sendPathLine);
@@ -242,7 +277,6 @@
Machine matchedMachine = machineService.getMachineBySendPathAnnotation(sendPathLine);
if (matchedMachine != null) {
- //vo.setName(parseProgramName(vo.getFilename()));
vo.setName(pnmameVO.logicProgramName());
vo.setMachineCode(matchedMachine.getCode());
--
Gitblit v1.9.3