java tomcat 5.5 - 读取资源文件的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/298521/
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-29 11:49:39  来源:igfitidea点击:

tomcat 5.5 - problem with reading resource files

javatomcatservletsresources

提问by Vladimir Prenner

I'm using Tomcat 5.5 as my servlet container. My web application deploys via .jar and has some resource files (textual files with strings and configuration parameters) located under its WEB-INF directory. Tomcat 5.5 runs on ubuntu linux. The resource file is read with a file reader:
fr = new FileReader("messages.properties");

我使用 Tomcat 5.5 作为我的 servlet 容器。我的 Web 应用程序通过 .jar 进行部署,并且在其 WEB-INF 目录下有一些资源文件(带有字符串和配置参数的文本文件)。Tomcat 5.5 在 ubuntu linux 上运行。使用文件阅读器读取资源文件:
fr = new FileReader("messages.properties");

The problem is that sometimes the servlet can't find the resource file, but if i restart it a couple of times it works, then again after some time it stops working. Can someone suggest what's the best way of reading resource strings from a servlet? Or a workaround for this problem? Putting the resource files under WEB-INF/classes doesn't help either.

问题是有时 servlet 找不到资源文件,但是如果我重新启动它几次它可以工作,然后在一段时间后它又停止工作。有人可以建议从 servlet 读取资源字符串的最佳方式是什么吗?或者这个问题的解决方法?将资源文件放在 WEB-INF/classes 下也无济于事。

采纳答案by diciu

I'm guessing the problem is you're trying to use a relative path to access the file. Using absolute path should help (i.e. "/home/tomcat5/properties/messages.properties").

我猜问题是您正在尝试使用相对路径来访问文件。使用绝对路径应该会有所帮助(即“/home/tomcat5/properties/messages.properties”)。

However, the usual solution to this problem is to use the getResourceAsStream method of the ClassLoader. Deploying the properties file to "WEB-INF/classes" will make it available to the class loader and you'll be able to access the properties stream.

然而,这个问题通常的解决方案是使用 ClassLoader 的 getResourceAsStream 方法。将属性文件部署到“WEB-INF/classes”将使其可用于类加载器,并且您将能够访问属性流。

Untested proto-code:

未经测试的原型代码:

Properties props = new Properties();

InputStream is =
getClass().getClassLoader().getResourceAsStream("messages.properties");

props.load(is);

回答by James Schek

If you are trying to access this file from a Servlet-aware class, such as a ContextListener or other lifecycle listener, you can use the ServletContext object to get the path to a resource.

如果您尝试从 Servlet 感知类(例如 ContextListener 或其他生命周期侦听器)访问此文件,则可以使用 ServletContext 对象来获取资源的路径。

These three are roughly equivalent. (Don't confuse getResourceAsStream as the same as the one provided by the ClassLoaderclass. They behave very differently)

这三者大致相当。(不要混淆 getResourceAsStream 与ClassLoader类提供的相同。它们的行为非常不同)

void myFunc(ServletContext context) {
   //returns full path. Ex: C:\tomcat.5\webapps\myapp\web-inf\message.properties 
   String fullCanonicalPath = context.getRealPath("/WEB-INF/message.properties");

   //Returns a URL to the file. Ex: file://c:/tomcat..../message.properties
   URL urlToFile = context.getResource("/WEB-INF/message.properties");

   //Returns an input stream. Like calling getResource().openStream();
   InputStream inputStream = context.getResourceAsStream("/WEB-INF/message.properties");
   //do something
}

回答by matt b

If you use

如果你使用

new FileReader("message.properties");

Then the FileReader will attempt to read that file from the base directory - which in Tomcat is likely to be the /bin folder.

然后 FileReader 将尝试从基目录读取该文件 - 在 Tomcat 中可能是 /bin 文件夹。

As diciu mentioned, use an absolute path or load it as a resource the classloader.

正如 diciu 所提到的,使用绝对路径或将其作为资源加载到类加载器中。

回答by Saulo Silva

I use the following code to load a properties file from within a servlet:

我使用以下代码从 servlet 中加载属性文件:

public void init(ServletConfig config) throws ServletException {
    String pathToFile = config.getServletContext().getRealPath("")
        + "/WEB-INF/config.properties";
    Properties properties = new Properties();
    properties.load(new FileInputStream(pathToPropertiesFile));
}

This works with Tomcat 6.0

这适用于 Tomcat 6.0

回答by Marcus Vinícius

I did used for Jboss Seam:

我确实用于 Jboss Seam:

ServletLifecycle.getServletContext().getRealPath("")

ServletLifecycle.getServletContext().getRealPath("")