【问题标题】:Relative path for getting file in Tomcats webapps directory from a deployed web application从已部署的 Web 应用程序中获取 Tomcats webapps 目录中文件的相对路径
【发布时间】:2023-03-05 10:52:01
【问题描述】:

我正在 TOMCAT 上部署一个 GWT webapp,它已经成功部署,war 文件在 webapps 目录中。现在应用程序需要访问文件“C:\apache-tomcat-7.0.42\webapps\TestHarness\modxml.xml”。当我提到相对路径时,即“..\TestHarness\modxml.xml”,应用程序无法找到文件.. 路径应该是什么,以便我的应用程序可以获取该 modxml.xml 文件。

【问题讨论】:

  • /TestHarness/modxml.xml 呢?
  • 我的 Web 应用程序需要访问位于文件夹 TestHarness 中的文件 modxml.xml,而 TestHarness 位于包含 .war 文件的同一文件夹中。
  • 好的,在这种情况下调用/TestHarness/modxml.xml 应该可以解决您的问题。
  • @MayurJadhav 您是从 servlet 访问文件吗?
  • 是的,我正在通过 servlet 访问..

标签: java gwt tomcat


【解决方案1】:

我建议将此文件作为资源从类路径访问,而不是直接通过文件系统访问。将文件添加到类路径上的目录中,例如源文件夹中的包。然后使用以下代码访问该文件:

File file = null;
try {
    file = new File(this.getClass().getResource("modxml.xml").toURI());
} catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

虽然我不推荐这种方法,但您可以使用request.getServletContext().getRealPath("/")HttpServletRequest 获取应用程序的位置。您可以将此值存储在字符串中,然后为文件添加路径后缀,并在 File 的构造函数中使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 2011-01-24
    • 2013-10-25
    相关资源
    最近更新 更多