- 相關推薦
Java獲得web的根路徑方法
引導語:用戶在磁盤上尋找文件時,所歷經的文件夾線路叫路徑。那么你知道運用Java如何獲取Web的根路徑嗎,以下是百分網小編分享給大家的Java獲得web的根路徑方法,歡迎閱讀!
java獲取根路徑有兩種方式:
1),在servlet可以用一下方法取得:
request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”)+”//upload//”;
2),不從jsp,或servlet中獲取,只從普通java類中獲取:
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
SAXReader() saxReader = new SAXReader();
if(path.indexOf(“WEB-INF”)>0){
path = path.substring(0,path.indexOf(“/WEB-INF/classes”)+16);
// ‘/WEB-INF/classes’為16位
document = saxReader.read(path+filename);
}else{
document = saxReader.read(getClass().getResourceAsStream(filename));
}
weblogic tomcat 下都有效
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
【Java獲得web的根路徑方法】相關文章:
Java編程中獲取路徑的方法12-16
Java與Java web的區(qū)別01-30
java實現web服務器的方法05-20
Java Web項目中解決中文亂碼的方法05-01
Java Web項目中解決中文亂碼方法05-07
Java與Java web有什么不同04-16
經典Java、Web筆試題04-27
java web基礎知識06-19