pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/se/simu/service/Impl/SemFilesSimuServiceImpl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/se/simu/utils/ZipUtils.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
pom.xml
@@ -167,7 +167,6 @@ <scope>compile</scope> </dependency> <!--webclient请æ±--> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-webflux --> <dependency> @@ -215,10 +214,13 @@ <artifactId>commons-fileupload</artifactId> <version>1.5</version> </dependency> </dependencies> <repositories> <repository> <id>jcenter</id> <url>https://jcenter.bintray.com/</url> </repository> </repositories> <build> <finalName>SimuServer</finalName> <plugins> src/main/java/com/se/simu/service/Impl/SemFilesSimuServiceImpl.java
@@ -4,22 +4,30 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.se.simu.service.SemFilesSimuService; import com.se.simu.utils.ZipUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.sql.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; @Slf4j @Service public class SemFilesSimuServiceImpl implements SemFilesSimuService { @Value("${config.outPath}") private String outPath; /** * è·å INTRODUCE @@ -69,12 +77,9 @@ Connection connection = connectToSQLiteWithCopy(filePath); System.out.println("SQLiteæ°æ®åºè¿æ¥æåï¼"); Statement stmt = connection.createStatement(); // æ¥è¯¢ENTITYè¡¨çæ°æ®å¹¶è¿åJSONæ ¼å¼çç»æ // result = queryEntityTable(stmt); result = queryDynamizersTable(stmt); // å ³éè¿æ¥ connection.close(); return result; @@ -222,7 +227,7 @@ * @return JSONæ ¼å¼çæ¥è¯¢ç»æ * @throws SQLException 妿æ¥è¯¢è¿ç¨ä¸åçé误 */ public static JSONObject queryDynamizersTable(Statement stmt) throws SQLException { public JSONObject queryDynamizersTable(Statement stmt) throws SQLException { // æå»ºSQLæ¥è¯¢è¯å¥ String querySql = "SELECT URL, GMLID, DATA FROM DYNAMIZERS"; // å建ä¸ä¸ª JSON 对象ç¨äºåå¨ç»æ @@ -237,17 +242,29 @@ jsonObject.put("URL", rs.getString("URL")); jsonObject.put("GMLID", rs.getString("GMLID")); // è·å BLOB æ°æ®å¹¶å°å ¶è½¬æ¢ä¸º Base64 ç¼ç å符串 String blobData = rs.getString("DATA"); // System.out.println("blobData = " + blobData); /* if (blobData != null) { // å° BLOB æ°æ®è½¬æ¢ä¸ºåèæ°ç» byte[] blobBytes = blobData.getBytes(1, (int) blobData.length()); // å°åèæ°ç»è½¬æ¢ä¸º Base64 å符串 String base64Data = Base64.getEncoder().encodeToString(blobBytes); // å° Base64 å符串æ¾å ¥ JSON 对象 jsonObject.put("DATA", base64Data); }*/ // å°æ¯ä¸è¡çç»ææ·»å å° JSON æ°ç»ä¸ byte[] blobData = rs.getBytes("DATA"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); String dirpath = outPath + "\\" + formatter.format(LocalDateTime.now()); String filepath = outPath + "\\" + formatter.format(LocalDateTime.now()) + "\\" + rs.getString("URL"); try { File file = new File(dirpath); if (!file.exists()) { file.mkdirs(); } File resultFile = new File(filepath); resultFile.createNewFile(); try (FileOutputStream fos = new FileOutputStream(filepath)) { fos.write(blobData); ZipUtils.unzip(filepath,dirpath); System.out.println("Bytes written to file successfully."); resultFile.delete(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } jsonObject.put("DATA", filepath); jsonArray.add(jsonObject); } // å°æ¥è¯¢ç»ææ¾å ¥æç»ç JSON å¯¹è±¡ä¸ src/main/java/com/se/simu/utils/ZipUtils.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,71 @@ package com.se.simu.utils; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ZipUtil; import org.apache.commons.io.FileUtils; import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; /** * zip */ public class ZipUtils { // è§£åzipæ ¼å¼ public static void unzip(String zipFilePath, String destDir){ //ææçæä»¶è·¯å¾ List<String> filePaths = new ArrayList<>(); File destDirectory = new File(destDir); if (!destDirectory.exists()) { if (!destDirectory.mkdirs()) { } } try (ZipFile zipFile = new ZipFile(zipFilePath)) { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String filePath = destDir + File.separator + entry.getName(); // ç¡®ä¿ç®æ æä»¶è·¯å¾çç¶ç®å½åå¨ File entryDestination = new File(filePath); File parentDirectory = entryDestination.getParentFile(); if (parentDirectory != null && !parentDirectory.exists()) { parentDirectory.mkdirs(); } if (!entry.isDirectory()) { // æåæä»¶ try (InputStream is = zipFile.getInputStream(entry); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) { byte[] bytesIn = new byte[4096]; int read; while ((read = is.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } } // æå°æä»¶è·¯å¾ filePaths.add(filePath); } else { // 妿æ¯ç®å½ï¼åå建ç®å½ï¼æ³¨æï¼å¨ä¸é¢çmkdirsè°ç¨ä¸å·²ç»å¤çäºï¼ // è¿éä¹å¯ä»¥éæ©æå°ç®å½è·¯å¾ } } } catch (IOException e) { e.printStackTrace(); try { File file = ZipUtil.unzip(zipFilePath, destDir, CharsetUtil.CHARSET_GBK); File[] file1 = file.listFiles(); for (int i = 0; i < file1.length; i++) { filePaths.add(file1[i].getAbsolutePath()); } } catch (Exception ex) { e.printStackTrace(); } } } }