简体   繁体   English

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

[英]tomcat 5.5 - problem with reading resource files

I'm using Tomcat 5.5 as my servlet container. 我正在使用Tomcat 5.5作为我的servlet容器。 My web application deploys via .jar and has some resource files (textual files with strings and configuration parameters) located under its WEB-INF directory. 我的Web应用程序通过.jar部署,并在其WEB-INF目录下有一些资源文件(带有字符串和配置参数的文本文件)。 Tomcat 5.5 runs on ubuntu linux. Tomcat 5.5在ubuntu linux上运行。 The resource file is read with a file reader: 使用文件读取器读取资源文件:
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. 问题是有时servlet找不到资源文件,但如果我重新启动它几次就可以了,那么经过一段时间它再次停止工作。 Can someone suggest what's the best way of reading resource strings from a servlet? 有人可以建议从servlet读取资源字符串的最佳方法是什么? Or a workaround for this problem? 或解决此问题的方法? Putting the resource files under WEB-INF/classes doesn't help either. 将资源文件放在WEB-INF / classes下也无济于事。

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 ClassLoader class. They behave very differently) (不要混淆getResourceAsStream与ClassLoader类提供的相同。它们的行为完全不同)

void myFunc(ServletContext context) {
   //returns full path. Ex: C:\tomcat\5.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
}

I'm guessing the problem is you're trying to use a relative path to access the file. 我猜你的问题是你正在尝试使用相对路径来访问该文件。 Using absolute path should help (ie "/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. 但是,此问题的常见解决方案是使用ClassLoader的getResourceAsStream方法。 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. 将属性文件部署到“WEB-INF / classes”将使其可用于类加载器,您将能够访问属性流。

Untested proto-code: 未经测试的原始代码:

Properties props = new Properties();

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

props.load(is);

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

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所提到的,使用绝对路径或将其作为类加载器的资源加载。

我用过Jboss Seam:

ServletLifecycle.getServletContext().getRealPath("")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM