GetResourceAsStream () always returns null

I have the following structure in a Java web application:

TheProject -- [Web Pages] -- -- [WEB-INF] -- -- -- abc.txt -- -- index.jsp -- [Source Packages] -- -- [wservices] -- -- -- WS.java 

In WS.java , I use the following code in a web method:

 InputStream fstream = this.getClass().getResourceAsStream("abc.txt"); 

But it always returns zero. I need to read from this file, and I read that if you put the files in WEB-INF , you can access them using getResourceAsStream , but the method always returns null .

Any ideas on what I might be doing wrong?

By the way, it is strange that this worked, but after I performed Clean and Build in the project, it suddenly stopped working: /

+39
java inputstream web-services jboss
May 09 '10 at 9:18 a.m.
source share
7 answers

As far as I know, the file should be right in the folder where the class 'this' , i.e. not in WEB-INF/classes , but nested even deeper (if you do not write in the package by default):

 net/domain/pkg1/MyClass.java net/domain/pkg1/abc.txt 

Entering a file into your java sources should work, the compiler copies this file along with the class files.

+35
May 09 '10 at 9:33 a.m.
source share

Call Class#getResourceAsStream(String) the class loader delegates and search for the resource in the class path. In other words, the current code will not work, and you must put abc.txt in WEB-INF/classes or in WEB-INF/lib if it is packaged in a jar file.

Or use ServletContext.getResourceAsStream(String) , which allows servlet containers to provide an accessible resource to a servlet from anywhere, without using a class loader. So use this from the servlet:

 this.getServletContext().getResourceAsStream("/WEB-INF/abc.txt") ; 



But is there a way that I can call getServletContext from my web service?

If you are using JAX-WS, you can get a WebServiceContext :

 @Resource private WebServiceContext wsContext; 

And then get the ServletContext from it:

 ServletContext sContext= wsContext.getMessageContext() .get(MessageContext.SERVLET_CONTEXT)); 
+28
May 09 '10 at 9:39 a.m.
source share

Instead

 InputStream fstream = this.getClass().getResourceAsStream("abc.txt"); 

using

 InputStream fstream = this.getClass().getClassLoader().getResourceAsStream("abc.txt"); 

This way it will look from the root, not from the path of the current calling class

+8
Dec 02 '15 at 8:16
source share

I think that you can get the file from "anywhere" (including the location of the servers), and you do not need to worry about where to put it.

This is usually a bad practice that should take care of such things.

 Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.properties"); 
+6
Jun 06 '13 at 8:39
source share

I don't know if this applies to JAX-WS, but for JAX-RS I was able to access the file by typing ServletContext and then calling getResourceAsStream () on it:

 @Context ServletContext servletContext; ... InputStream is = servletContext.getResourceAsStream("/WEB-INF/test_model.js"); 

Note that, at least in GlassFish 3.1, the path must be absolute, i.e. start with a slash. Read more here: How to use properties file with jax-rs?

+5
Feb 28 2018-12-12T00:
source share

I had the same problem when I switched from Websphere 8.5 to WebSphere Liberty.

I used FileInputStream instead of getResourceAsStream() , because for some reason WebSphere Liberty could not find the file in the WEB-INF folder.

script:

 FileInputStream fis = new FileInputStream(getServletContext().getRealPath("/") + "\WEBINF\properties\myProperties.properties") 

Note: I used this script for development only.

+1
Jul 02 '14 at 16:57
source share

I had a similar problem, and I searched for a solution for a long time: It looks like the string parameter is case sensitive. Therefore, if your file name is abc.TXT, but you are looking for abc.txt, eclipse will find it - the executable JAR file will not.

0
Feb 15 '16 at 8:26
source share



All Articles