二话不说,直接上代码
package cn.sigangjun.util;
/**
* <p>Title: Get Path Util </p>
* <p>Description: this class can getWebClassesPath,getWebInfPath and getWebRoot</p>
* @since 2013-8-12 下午4:09:00
* @version 1.0
* @author <a style='color:red' href='http://blog.csdn.net/sigangjun'>sigangjun</a>
*/
public class PathUtil {
public String getWebClassesPath() {
String path = getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath();
path = trim(path);
return path;
}
public String getWebInfPath() throws IllegalAccessException {
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(0, path.indexOf("WEB-INF") + 8);
path = trim(path);
} else {
throw new IllegalAccessException("路径获取错误");
}
return path;
}
public String getWebRoot() throws IllegalAccessException {
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(0, path.indexOf("WEB-INF/classes"));
path = trim(path);
} else {
throw new IllegalAccessException("路径获取错误");
}
return path;
}
private String trim(String s){
if(s.startsWith("/")||s.startsWith("\\")){
s = s.substring(1);
trim(s);
}
return s;
};
public static void main(String[] args) throws Exception {
PathUtil p = new PathUtil();
System.out.println(p.getWebClassesPath());
System.out.println(p.getWebInfPath());
System.out.println(p.getWebRoot());
}
}