转:java获取根路径有两种方式
http://java.chinaitlab.com/net/818899.html
java获取根路径有两种方式:
1),在servlet可以用一下方法取得:
request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”)+”//upload//”;
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)); }
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();