| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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())); |
| | | } |
| | | |
| | |
| | | * @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("使用编码 {} 解析成功 ",encoding); |
| | | break; |
| | | } catch (Exception e) { |
| | | log.error("使用编码 {} 解析失败: ",encoding,e); |
| | | } |
| | | } |
| | | } |
| | | public void extractZipToTempDirWithCharset(Path zipFilePath, Path extractDir, Charset charset) throws IOException { |
| | | // 获取系统临时目录 |
| | | String tempDir = System.getProperty("java.io.tmpdir"); |
| | | |
| | | // 创建解压目标目录(在临时目录下创建一个唯一子目录) |
| | | try (InputStream fis = Files.newInputStream(zipFilePath); |
| | | ZipInputStream zis = new ZipInputStream(fis)) { |
| | | ZipInputStream zis = new ZipInputStream(fis,charset)) { |
| | | |
| | | ZipEntry zipEntry = zis.getNextEntry(); |
| | | while (zipEntry != null) { |
| | |
| | | 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)); |
| | | } |
| | |
| | | }*/ |
| | | 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编码解析 |
| | | 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)) { |
| | | // 使用输入流读取文件内容 |
| | |
| | | |
| | | 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); |
| | |
| | | Machine matchedMachine = machineService.getMachineBySendPathAnnotation(sendPathLine); |
| | | |
| | | if (matchedMachine != null) { |
| | | //vo.setName(parseProgramName(vo.getFilename())); |
| | | vo.setName(pnmameVO.logicProgramName()); |
| | | vo.setMachineCode(matchedMachine.getCode()); |
| | | |