| | |
| | | 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 |
| | |
| | | Connection connection = connectToSQLiteWithCopy(filePath); |
| | | System.out.println("SQLite数据库连接成功!"); |
| | | Statement stmt = connection.createStatement(); |
| | | |
| | | |
| | | // 查询ENTITY表的数据并返回JSON格式的结果 |
| | | // result = queryEntityTable(stmt); |
| | | result = queryDynamizersTable(stmt); |
| | | |
| | | // 关闭连接 |
| | | connection.close(); |
| | | return result; |
| | |
| | | * @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 对象用于存储结果 |
| | |
| | | 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 对象中 |