Java getResourceAsStream() 总是返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2797162/
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
getResourceAsStream() is always returning null
提问by Andreas Grech
I have the following structure in a Java Web Application:
我在 Java Web 应用程序中有以下结构:
TheProject
-- [Web Pages]
-- -- [WEB-INF]
-- -- -- abc.txt
-- -- index.jsp
-- [Source Packages]
-- -- [wservices]
-- -- -- WS.java
In WS.java
, I am using the following code in a Web Method:
在 中WS.java
,我在 Web 方法中使用以下代码:
InputStream fstream = this.getClass().getResourceAsStream("abc.txt");
But it is always returning a null. I need to read from that file, and I read that if you put the files in WEB-INF
, you can access them with getResourceAsStream
, yet the method is always returning a null
.
但它总是返回一个空值。我需要从文件中读取,和我读,如果你把这些文件中WEB-INF
,你可以访问它们getResourceAsStream
,但该方法总是返回null
。
Any ideas of what I may be doing wrong?
关于我可能做错了什么的任何想法?
Btw, the strange thing is that this was working, but after I performed a Clean and Build
on the Project, it suddenly stopped working :/
顺便说一句,奇怪的是,这是有效的,但是在我Clean and Build
对项目执行 a 后,它突然停止工作:/
采纳答案by Jaroslav Záruba
To my knowledge the file has to be right in the folder where the 'this'
class resides, i.e. not in WEB-INF/classes
but nested even deeper (unless you write in a default package):
据我所知,该文件必须'this'
位于类所在的文件夹中,即不在其中,WEB-INF/classes
而是嵌套得更深(除非您在默认包中编写):
net/domain/pkg1/MyClass.java
net/domain/pkg1/abc.txt
Putting the file in to your java sources should work, compiler copies that file together with class files.
将文件放入您的 java 源代码应该可以工作,编译器将该文件与类文件一起复制。
回答by Pascal Thivent
A call to Class#getResourceAsStream(String)
delegates to the class loader and the resource is searched in the class path. In other words, you current code won't work and you should put abc.txt
in WEB-INF/classes
, or in WEB-INF/lib
if packaged in a jar file.
调用Class#getResourceAsStream(String)
委托给类加载器,并在类路径中搜索资源。换句话说,你当前的代码将无法正常工作,你应该把abc.txt
在WEB-INF/classes
,或者WEB-INF/lib
如果打包在一个jar文件。
Oruse ServletContext.getResourceAsStream(String)
which allows servlet containers to make a resource available to a servlet from any location, without using a class loader. So use this from a Servlet:
或者使用ServletContext.getResourceAsStream(String)
它允许 servlet 容器从任何位置为 servlet 提供资源,而无需使用类加载器。所以从 Servlet 中使用它:
this.getServletContext().getResourceAsStream("/WEB-INF/abc.txt") ;
But is there a way I can call getServletContext from my Web Service?
但是有没有一种方法可以从我的 Web 服务中调用 getServletContext?
If you are using JAX-WS, then you can get a WebServiceContext
injected:
如果您使用的是 JAX-WS,那么您可以获得WebServiceContext
注入:
@Resource
private WebServiceContext wsContext;
And then get the ServletContext
from it:
然后从中获取ServletContext
:
ServletContext sContext= wsContext.getMessageContext()
.get(MessageContext.SERVLET_CONTEXT));
回答by Matthew Cornell
I don't know if this applies to JAX-WS, but for JAX-RS I was able to access a file by injecting a ServletContext and then calling getResourceAsStream() on it:
我不知道这是否适用于 JAX-WS,但对于 JAX-RS,我能够通过注入 ServletContext 然后在其上调用 getResourceAsStream() 来访问文件:
@Context ServletContext servletContext;
...
InputStream is = servletContext.getResourceAsStream("/WEB-INF/test_model.js");
Note that, at least in GlassFish 3.1, the path had to be absolute, i.e., start with slash. More here: How do I use a properties file with jax-rs?
请注意,至少在 GlassFish 3.1 中,路径必须是绝对路径,即以斜杠开头。更多信息:如何在 jax-rs 中使用属性文件?
回答by jediz
I think this way you can get the file from "anywhere" (including server locations) and you do not need to care about where to put it.
我认为这样你可以从“任何地方”(包括服务器位置)获取文件,你不需要关心把它放在哪里。
It's usually a bad practice having to care about such things.
不得不关心这些事情通常是一种不好的做法。
Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.properties");
回答by Ciro Hidalgo
I had the same problem when I changed from Websphere 8.5 to WebSphere Liberty.
当我从 Websphere 8.5 更改为 WebSphere Liberty 时,我遇到了同样的问题。
I utilized FileInputStream
instead of getResourceAsStream()
, because for some reason WebSphere Liberty can't locate the file in the WEB-INF
folder.
我使用了FileInputStream
代替getResourceAsStream()
,因为出于某种原因,WebSphere Liberty 无法在文件WEB-INF
夹中找到该文件。
The script was :
脚本是:
FileInputStream fis = new FileInputStream(getServletContext().getRealPath("/")
+ "\WEBINF\properties\myProperties.properties")
Note: I used this script only for development.
注意:我仅将此脚本用于开发。
回答by Mike-Bell
Instead of
代替
InputStream fstream = this.getClass().getResourceAsStream("abc.txt");
use
用
InputStream fstream = this.getClass().getClassLoader().getResourceAsStream("abc.txt");
In this way it will look from the root, not from the path of the current invoking class
这样,它将从根查看,而不是从当前调用类的路径查看
回答by Dominik Maresch
I had a similar problem and I searched for the solution for quite a while: It appears that the string parameter is case sensitive. So if your filename is abc.TXT but you search for abc.txt, eclipse will find it - the executable JAR file won't.
我有一个类似的问题,我搜索了很长一段时间的解决方案:字符串参数似乎区分大小写。因此,如果您的文件名是 abc.TXT 但您搜索 abc.txt,eclipse 会找到它 - 可执行 JAR 文件不会。