张洋洋
2025-01-08 29723c0c6658effce1419aadc01354853965ec6d
[add]h5读取改造
已添加1个文件
已修改2个文件
128 ■■■■ 文件已修改
pom.xml 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/Impl/SemFilesSimuServiceImpl.java 47 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/utils/ZipUtils.java 71 ●●●●● 补丁 | 查看 | 原始文档 | 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();
            }
        }
    }
}