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 支付宝

支付宝