获取web项目的根路径

经验:

getRootPath1在tomcat和weblogic以war包发布有效

getRootPath1在tomcat和weblogic以文件夹发布有效


public static String getRootPath1() {
return System.getProperty("miugonavi.root");
}


public static String getRootPath2() {
String classPath = null;
try {
//toURI() 20% to 空格
classPath = CommonUtil.class.getClassLoader().getResource("/").toURI().getPath();
} catch (URISyntaxException e) {

e.printStackTrace();
}

String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
return rootPath+File.separator;
}

原文地址:https://www.cnblogs.com/yangy608/p/2736075.html