获取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;
}

posted @ 2012-10-23 19:25  七郎  Views(2695)  Comments(0Edit  收藏  举报