獲取當前所在的項目的WebRoot根路徑的方法
/**
* 獲取當前所在的項目的WebRoot根路徑
* @return
*/
private String getWebInfPath(){
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
String path = url.toString();
int index = path.indexOf("WEB-INF");
if(index == -1){
index = path.indexOf("classes");
}
if(index == -1){
index = path.indexOf("bin");
}
path = path.substring(0, index);
if(path.startsWith("zip")){//當class文件在war中時,此時返回zip:D:/...這樣的路徑
path = path.substring(4);
}else if(path.startsWith("file")){//當class文件在class文件中時,此時返回file:/D:/...這樣的路徑
path = path.substring(6);
}else if(path.startsWith("jar")){//當class文件在jar文件裏面時,此時返回jar:file:/D:/...這樣的路徑
path = path.substring(10);
}
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return path;
}
* 獲取當前所在的項目的WebRoot根路徑
* @return
*/
private String getWebInfPath(){
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
String path = url.toString();
int index = path.indexOf("WEB-INF");
if(index == -1){
index = path.indexOf("classes");
}
if(index == -1){
index = path.indexOf("bin");
}
path = path.substring(0, index);
if(path.startsWith("zip")){//當class文件在war中時,此時返回zip:D:/...這樣的路徑
path = path.substring(4);
}else if(path.startsWith("file")){//當class文件在class文件中時,此時返回file:/D:/...這樣的路徑
path = path.substring(6);
}else if(path.startsWith("jar")){//當class文件在jar文件裏面時,此時返回jar:file:/D:/...這樣的路徑
path = path.substring(10);
}
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return path;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.