具体获取方式:
/**
*
* @return WebRoot目录的绝对路径
*/
public static String getWebRootAbsolutePath() {
String path = null;
String folderPath = Path.class.getProtectionDomain().getCodeSource()
.getLocation().getPath();
if (folderPath.indexOf("WEB-INF") > 0) {
path = folderPath.substring(0, folderPath
.indexOf("WEB-INF/classes"));
}
return path;
}
本文介绍了一种在Java中获取Web应用程序的WebRoot目录绝对路径的方法。通过利用Path类的getProtectionDomain方法结合CodeSource的getLocation方法来实现路径的获取。如果路径中包含WEB-INF,则截取到WEB-INF/classes之前的部分作为WebRoot的绝对路径。
138

被折叠的 条评论
为什么被折叠?



