0%

獲取項目的根目錄絕對路徑

獲取項目(c/s)的根目錄絕對路徑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.net.URLDecoder;

public class UtilityHelper {
/**
* 獲取項目(c/s)的根目錄絕對路徑
* @return
*/
public static String getClientRootAbsoluteClassPath()
{
String path = null;
try {
path = UtilityHelper.class.getProtectionDomain().getCodeSource().getLocation().getFile();
path = URLDecoder.decode(path, "utf-8");
path = path.replace('\\', '/');
path = path.substring(0,path.lastIndexOf("/")) + "/";
logger.info("path:" + path);

} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(),e);
// e.printStackTrace();
}
return path;
}
}

獲取項目(B/s)的根目錄絕對路徑

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
/**
* 得到classPath絕對路徑
* @param classPath
* 相對路徑
* @return
*/
public static String getAbsoluteClassPath(String classPath) {

String path = null;
try {
File file = new File(Thread.currentThread().getContextClassLoader().getResource(classPath).getPath());
path = URLDecoder.decode(file.getAbsolutePath(), "utf-8");
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
// e.printStackTrace();
}
return path;
}

/**
* 獲取項目的根目錄絕對路徑
* @return
*/
public static String getRootAbsoluteClassPath() {
String absoluteClassPath = getAbsoluteClassPath();
//獲取根目錄的絕對路徑
return absoluteClassPath.substring(0,absoluteClassPath.indexOf("WEB-INF")).replace('\\', '/');

}
layicr 微信支付

微信支付

layicr 支付寶

支付寶