首先得到webRoot 的路径:
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = ClassLoader.getSystemClassLoader();
}
java.net.URL url = classLoader.getResource("");
String classPath = url.getPath() + "/";//classPath所在的路径
File rootFile = new File(classPath);
String webInfoPath = rootFile.getParent() + "/"; //webInfo所在的路径
File webInfoDir = new File(webInfoPath);
String webRootPATH = webInfoDir.getParent() + "/"; //webroot所在的路径
其次直接引用所需文件的路径。
本文介绍了一种在Java中获取Web应用根目录(webRoot)路径的方法,并演示了如何利用类加载器来找到项目的根目录,进而引用所需的文件路径。
1182

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



