java Servlet/Tomcat 相对文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10680637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:04:38  来源:igfitidea点击:

Servlet/Tomcat relative file path

javajsptomcatservlets

提问by Betamoo

Where to put files in a Tomcat Servlet application, such that they are relatively visible to the page??

Tomcat Servlet 应用程序中文件放在哪里,以便它们对页面相对可见??

More detail: I am developing a servletpage while using an external library. That library depends massively on external loaded XML files(relative file paths). So I need to put these XML files in the running directoryof the servlet.

更多详细信息:我正在使用外部库开发servlet页面。该库在很大程度上依赖于外部加载的 XML 文件(相对文件路径)。所以我需要把这些XML文件放在servlet的运行目录下。

Is there a way in Tomcat server, where files can be accessible relatively?

Tomcat服务器有没有办法可以相对访问文件?

回答by Zach

When a web application is deployed to Tomcat, the root of the web application ends up being $CATALINA_HOME/webapps/YOUR_WEB_APP/

当 Web 应用程序部署到 Tomcat 时,Web 应用程序的根目录最终是 $CATALINA_HOME/webapps/YOUR_WEB_APP/

As such, if using a servlet to access an XML file located on a path within the root of your web application, you can just use the following:

因此,如果使用 servlet 访问位于 Web 应用程序根目录中路径上的 XML 文件,则只需使用以下内容:

request.getServletContext().getResourceAsStream("PATH/TO/YOUR/XML_FILE.xml")

This will load the XML file as an InputStream. Of course, if you want access to the file itself, you can always use the getResource(String resource)method to obtain a URL, from which a Fileobject can be obtained like so (alternative methods included):

这会将 XML 文件加载为InputStream. 当然,如果你想访问文件本身,你总是可以使用getResource(String resource)获取 a的方法URL,从中File可以像这样获取一个对象(包括替代方法):

File f;
try {
  f = new File(url.toURI());
} catch(URISyntaxException e) {
  f = new File(url.getPath());
}

EDIT: To make them relatively visible to a web browser, simply keep them out of the ./WEB-INF and ./META-INF directories.

编辑:为了使它们对 Web 浏览器相对可见,只需将它们放在 ./WEB-INF 和 ./META-INF 目录之外。

回答by W. Goeman

If this library you are talking about is going to search for the file on the classpath (like Hibernate or Log4J would do), you will have to put your XML file in WEB-INF. However, I suggest you do not do this manually. You can put the file in a source directory of you application, which will make sure the file gets deployed on the right spot.

如果您正在谈论的这个库要在类路径上搜索文件(如 Hibernate 或 Log4J 会这样做),您将不得不将您的 XML 文件放在 WEB-INF 中。但是,我建议您不要手动执行此操作。您可以将文件放在应用程序的源目录中,这将确保文件部署在正确的位置。

回答by Roger F. Gay

This is an old question. I'm working on Tomcat 9. I've been quite successful with taking the Tomcat installation directory as the base. (CATALINA_HOME) The relative path to a file in ROOT for example is then, "webapps/ROOT/someDir/fileName"

这是一个老问题。我正在研究 Tomcat 9。我已经非常成功地将 Tomcat 安装目录作为基础。(CATALINA_HOME) 例如,ROOT 中文件的相对路径是“webapps/ROOT/someDir/fileName”

The place to complain about repeated answers is to deal with repeated questions.

抱怨重复答案的地方是处理重复问题。