[英]Java/Tomcat: ServletContext & getResourceAsStream Problems
I am trying to access a conf file (located in the WEB-INF folder) from a Tomcat web app. 我正在尝试从Tomcat Web应用程序访问conf文件(位于WEB-INF文件夹中)。 At the moment, I have the location of the file hard coded as a String
. 此刻,我已将文件的位置硬编码为String
。 However, this does not work when the tomcat/webapps folder is in a different location than my hard coded String
indicates. 但是,当tomcat / webapps文件夹与我的硬编码String
指示的位置不同时,这将不起作用。 I've looked online and it seems like using the getResourceAsStream ()
method is what I'm looking for, but I'm having a hard time getting it to work. 我在网上看过,似乎正在使用getResourceAsStream ()
方法,但我很难使它开始工作。 My application is not liking it when I call the getServletContext ()
method. 调用getServletContext ()
方法时,我的应用程序不喜欢它。 Can anyone help me? 谁能帮我?
EDIT: The relevant block of code 编辑:相关的代码块
BufferedReader myReader = new BufferedReader (new InputStreamReader (getServletContext ().getResourceAsStream ("/WEB-INF/conf.txt")));
To solve this problem I created two folders under WEB-INF (path/to/app/WEB-INF/classes/mypackage/) and then put my files in this folder. 为了解决此问题,我在WEB-INF下创建了两个文件夹(path / to / app / WEB-INF / classes / mypackage /),然后将文件放在此文件夹中。 Then, from my POJO I called this.getClass.getResourceAsStream ("<filename>")
to open up a stream. 然后,从我的POJO中调用this.getClass.getResourceAsStream ("<filename>")
打开流。 To just get a String that was the complete absolute path name of a file I did this.getClass.getResource ("<filename>").toString ().substring (5)
. 为了只获取一个字符串,它是文件的完整绝对路径名,我做了this.getClass.getResource ("<filename>").toString ().substring (5)
。
If you are trying to load a file from the WEB-INF directory in Tomcat, use the following code: 如果要尝试从Tomcat的WEB-INF目录加载文件,请使用以下代码:
For example, for a file in WEB-INF/config/config.xml 例如,对于WEB-INF / config / config.xml中的文件
ServletContext context = ....//get servlet context
InputStream is = context.getResourceAsStream("/WEB-INF/config/config.xml");
If your config file is at /WEB-INF/config.xml, you could access it from a servlet by using "../config.xml" instead of a full path. 如果您的配置文件位于/WEB-INF/config.xml,则可以使用“ ../config.xml”而不是完整路径从servlet访问它。 This works because compiled application classes are normally located within /WEB-INF/classes. 这是可行的,因为编译的应用程序类通常位于/ WEB-INF / classes中。
Following code worked for me. 以下代码为我工作。 I kept my config
file under WEB-INF/
我将config
文件保存在WEB-INF/
web.xml code snippet: web.xml代码段:
<servlet>
<servlet-name>wizardcontroller</servlet-name>
<servlet-class>com.sg.poc.wizard.controller.CustomerWizardController</servlet-class>
<init-param>
<param-name>plm-config</param-name>
<param-value>plm-config.xml</param-value>
</init-param>
</servlet>
Servlet code snippet: Servlet代码段:
String configfile = config.getInitParameter("plm-config") ;
InputStream is = config.getServletContext().getResourceAsStream("/WEB-INF/"+configfile);
System.out.println("Resource Stream is : "+is);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.