tomcat webapps目录的绝对路径?

时间:2009-08-05 11:10:29

标签: tomcat web-applications

我在一个地方提取tomcat,我的webapps目录可能在其他地方。那么如何获取我的Web应用程序的绝对路径? 。我在webapps中有我的文件处理程序,我想找到我的webapps或我的应用程序目录的绝对路径(从c:/或/ home / use / ../../webapp/mywebapplication开始)。事情是我无法控制我的应用程序将在何处/何处部署?

2 个答案:

答案 0 :(得分:8)

答案 1 :(得分:2)

  

请参阅getServletContext()。getRealPath(“”) - 如果从.war存档中提供内容,这种方式将无效。

在某些情况下,我们可以使用另一种方式来获得真正的路径。这是获取属性文件C的路径的示例:/ Program Files / Tomcat 6 / webapps / myapp / WEB-INF / classes / somefile.properties:

// URL returned "/C:/Program%20Files/Tomcat%206.0/webapps/myapp/WEB-INF/classes/"
URL r = this.getClass().getResource("/");

// path decoded "/C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
String decoded = URLDecoder.decode(r.getFile(), "UTF-8");

if (decoded.startsWith("/")) {
    // path "C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
    decoded = decoded.replaceFirst("/", "");
}
File f = new File(decoded, "somefile.properties");
相关问题