【问题标题】:tomcat webapps directory absolute path?tomcat webapps目录绝对路径?
【发布时间】:2009-08-05 11:10:29
【问题描述】:

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

【问题讨论】:

    标签: tomcat web-applications


    【解决方案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");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-04
        • 2012-07-21
        • 2011-01-15
        • 2014-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多