public class WebPathUtil{
//取得/WebRoot/WEB-INF/classes/
public String getWebClassesPath() {
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
return path;
}
//取得/WebRoot/WEB-INF/
public String getWebInfPath() throws IllegalAccessException{
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(0, path.indexOf("WEB-INF")+8);
} else {
throw new IllegalAccessException("路径获取错误");
}
return path;
}
//测试
public static void main(String[] args)throws Exception {
WebPathUtil p = new WebPathUtil();
System.out.println(p.getWebClassesPath());
System.out.println(p.getWebInfPath());
System.out.println(p.getWebRoot());
}
}
普通java类获取web应用的根目录
最新推荐文章于 2023-02-15 00:24:40 发布