import java.io.File;
/**
* @author SiPei.Qiao
* @function 應用根路徑整理類
*/
public class PathUtil {
/**
* @function 得到類所在的本地目錄
* @return
*/
public String getWebClassesPath() {
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
return path;
}
/**
* 得到WEB-INF根目錄
*
* @return
* @throws IllegalAccessException
*/
public String getWebInfPath() throws IllegalAccessException {
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
if ("\\".equals(File.separator)) {
path = path.substring(0, path.indexOf("WEB-INF") + 8);
path = path.substring(1, path.length());
// path = path.replaceAll("/", "\\");
// System.out.println(path);
} else {
path = path.substring(0, path.indexOf("WEB-INF") + 8);
}
} else {
throw new IllegalAccessException("Can't get WebInfo path");
}
return path;
}
/**
* @function 得到應用的根目錄
* @return
* @throws IllegalAccessException
*/
public String getWebRoot() throws IllegalAccessException {
String path = getWebClassesPath();
if (path.indexOf("WEB-INF") > 0) {
String keyWord = "WEB-INF";
if ("\\".equals(File.separator)) {
path = path.substring(1, path.indexOf(keyWord));
} else {
path = path.substring(0, path.indexOf(keyWord));
}
} else {
throw new IllegalAccessException("Can't get WebRoot path");
}
return path;
}
/**
* @function 得到應用的根目錄
* @return
*/
public String getWebRootEx() {
try {
return getWebRoot();
} catch (Exception e) {
return null;
}
}
}
應用根路徑整理類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.