Bläddra i källkod

解决紫金报价报错找不到文件问题

wangrq 1 månad sedan
förälder
incheckning
9c5239f898

+ 42 - 9
tenant/insurance/quotation-zijin/src/main/java/com/jzg/quotation/zijin/crawler/util/RS6Item.java

@@ -13,12 +13,15 @@ import org.jsoup.select.Elements;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.UncheckedIOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.FileSystemException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
@@ -49,17 +52,45 @@ public class RS6Item {
         this.headers = new HashMap<>();
         this.headersForApi = new HashMap<>();
         this.cookies = new LinkedHashMap<>();
-        // 设置scriptDir为项目根目录的绝对路径
-        this.scriptDir = getProjectRootPath();
+        // 从classpath提取JS文件到临时目录,兼容本地开发和Docker容器环境
+        this.scriptDir = initScriptDir();
         initializeHeaders();
     }
 
-    private String getProjectRootPath() {
+    /**
+     * 创建临时目录并从classpath提取JS静态资源文件
+     * 解决Docker容器中无源码目录导致的NoSuchFileException
+     * @return 临时目录的绝对路径
+     */
+    private String initScriptDir() {
         try {
-            File projectRoot = new File("tenant/insurance/quotation-zijin/src/main/resources/js");
-            return projectRoot.getAbsolutePath();
-        } catch (Exception e) {
-            return ".";
+            Path tempDir = Files.createTempDirectory("zijin-js-");
+            // 确保JVM退出时清理临时目录
+            tempDir.toFile().deleteOnExit();
+
+            // 从classpath复制所有JS文件到临时目录
+            String[] jsFiles = {"main.js", "auto.js", "content.js", "tsssss.js", "ts.js"};
+            for (String fileName : jsFiles) {
+                copyResourceToTemp("js/" + fileName, tempDir.resolve(fileName));
+            }
+
+            log.info("紫金JS脚本目录已初始化: {}", tempDir);
+            return tempDir.toString();
+        } catch (IOException e) {
+            throw new UncheckedIOException("初始化紫金JS脚本临时目录失败", e);
+        }
+    }
+
+    /**
+     * 从classpath复制单个资源文件到目标路径
+     */
+    private void copyResourceToTemp(String resourcePath, Path targetPath) throws IOException {
+        try (InputStream is = getClass().getClassLoader().getResourceAsStream(resourcePath)) {
+            if (is == null) {
+                log.warn("classpath中未找到资源: {}, 跳过复制", resourcePath);
+                return;
+            }
+            Files.copy(is, targetPath, StandardCopyOption.REPLACE_EXISTING);
         }
     }
 
@@ -309,6 +340,8 @@ public class RS6Item {
      */
     private void writeJsFile(String filename, String content) throws IOException {
         Path filePath = Paths.get(scriptDir, filename);
+        // 确保父目录存在
+        Files.createDirectories(filePath.getParent());
         int maxRetries = 3;
         long retryDelayMs = 500;
         
@@ -379,7 +412,7 @@ public class RS6Item {
         }
 
         /*RS6Item rs6Item = createRS6Instance();
-        String projectRootPath = rs6Item.getProjectRootPath();
-        System.out.println(projectRootPath);*/
+        String scriptDir = rs6Item.scriptDir;
+        System.out.println(scriptDir);*/
     }
 }