Răsfoiți Sursa

修复PathUtil在spring-boot lib包下获取位置不准的bug

(cherry picked from commit 437d582416a3206ff07eaa2996fd2d1ec8e0ce58)
JustryDeng 4 ani în urmă
părinte
comite
65689d9de4

+ 21 - 1
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/util/PathUtil.java

@@ -33,7 +33,15 @@ public final class PathUtil {
         try {
             filePath = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8.name());
         } catch (UnsupportedEncodingException e) {
-            throw new ClassWinterException(e);
+            throw new RuntimeException(e);
+        }
+        /*
+         * C:\Users\JustryDeng\Desktop\file:\E:\Java\demo\Abc_SpringBoot_Demo\target\spring-boot-demo-0.0.1-SNAPSHOT.jar
+         * 转换为
+         * \E:\Java\demo\Abc_SpringBoot_Demo\target\spring-boot-demo-0.0.1-SNAPSHOT.jar
+         */
+        if (filePath.contains("file:")) {
+            filePath = filePath.substring(filePath.indexOf("file:") + "file:".length());
         }
         File file = new File(filePath);
         filePath = file.getAbsolutePath().replace("\\", "/");
@@ -41,6 +49,18 @@ public final class PathUtil {
         if (file.isDirectory() && !filePath.endsWith("/")) {
             filePath = filePath + "/";
         }
+        // spring-boot jar包里的class, 获取到的形如: your-spring-boot.jar!/BOOT-INF/classes!
+        String springBootJarInnerClass = "!/BOOT-INF/classes!";
+        if (filePath.endsWith(springBootJarInnerClass)) {
+            filePath = filePath.substring(0, filePath.length() - springBootJarInnerClass.length());
+        }
+        // spring-boot jar包里jar包里的class, 获取到的形如: your-spring-boot.jar!/BOOT-INF/lib/your-lib-jar.jar!
+        String springBootJarInnerJar = "!/BOOT-INF/lib";
+        int idx = filePath.indexOf(springBootJarInnerJar);
+        if (idx >= 0) {
+            filePath = filePath.substring(0, idx);
+        }
+    
         return filePath;
     }
 }