Path.java
1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.canrd.webmagic.common.directory;
import com.canrd.webmagic.common.constant.Constant;
/**
* @author xms
*/
public class Path {
/**
* 获取程序启动路径
*
* @param cls,建议直接传 ServiceApplication.class
* @return 应用启动路径
*/
public static String getAppPath(Class cls) {
String path = cls.getResource(Constant.SLASH_MARK_CHARACTER).getPath();
String os = System.getProperty("os.name").toLowerCase();
final String OSWINDOW = "windows";
if (os.indexOf(OSWINDOW) != -1 && path.length() > 1) {
//windows路径样例: /D:/work/code/shop-services/online-shop-oss/target/classes/
return path.substring(1);
}
//linux 路径样例 file:/opt/target/online-shop-oss-SNAPSHOT.jar!/BOOT-INF/classes!/";
final String FILE = "file:";
if (path.indexOf(FILE) != -1) {
path = path.substring(FILE.length());
}
int pos = -1;
if ((pos = path.indexOf(Constant.EXCLAMATION_MARK_CHARACTER)) != -1) {
if (-1 != (pos = path.lastIndexOf(Constant.SLASH_MARK_CHARACTER, pos))) {
path = path.substring(0, pos + 1);
}
}
return path;
}
}