MCPcopy Create free account
hub / github.com/diyhi/bbs / readPath

Method readPath

src/main/java/cms/utils/PathUtil.java:39–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

37 }
38
39 private static void readPath() {
40 ApplicationHome home = new ApplicationHome(Application.class);
41 File source = home.getSource();
42 String path = "";
43 if (source != null) {//服务运行环境
44 // 返回 JAR 包所在的文件夹
45 path = source.getParentFile().getAbsolutePath();
46 }else{//打包环境
47
48 // 寻找 target 目录
49 // 获取当前类文件的实际物理路径(可能是 .../target/classes/cms/Application.class)
50 String classPath = Application.class.getProtectionDomain().getCodeSource().getLocation().getPath();
51
52 // 解码 URL(处理路径中的空格或中文字符)
53 classPath = java.net.URLDecoder.decode(classPath, StandardCharsets.UTF_8);
54 File classFile = new File(classPath);
55
56 // 向上递归寻找名为 "target" 的文件夹
57 File current = classFile;
58 while (current != null) {
59 if (current.getName().equalsIgnoreCase("target")) {
60 path = current.getAbsolutePath();
61 break;
62 }
63 current = current.getParentFile();
64 }
65
66 // 如果没找到 target(比如在极端的非 Maven 环境下)
67 if (path.isEmpty()) {
68 path = System.getProperty("user.dir");
69 }
70 }
71
72
73 //File jarFile = home.getDir();
74 //String path = jarFile.getParentFile().toString();
75 rootPath = path + File.separator+"classes";//F:\JAVA\bbs-pro-jdk21\target\classes
76 //论坛外部目录
77 Object externalDirectory = YmlUtils.getYmlProperty("application.yml","bbs.externalDirectory");
78 if(externalDirectory != null && !externalDirectory.toString().trim().isEmpty()){//如果已设置了论坛外部目录 G:\bbs
79 defaultExternalDirectory = externalDirectory.toString();
80 }else{
81 defaultExternalDirectory = path + File.separator + "bbs";//F:\JAVA\bbs-pro-jdk21\target\bbs
82 }
83 }
84
85 /**
86 * 自动路径 jar启动时使用外部路径 IDE启动时使用内部路径

Callers 2

rootPathMethod · 0.95

Calls 7

getYmlPropertyMethod · 0.95
getPropertyMethod · 0.80
getPathMethod · 0.45
decodeMethod · 0.45
getNameMethod · 0.45
isEmptyMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected