getResourceAsStream()始终返回null [英] getResourceAsStream() is always returning null
问题描述
我在Java Web应用程序中有以下结构:
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
,我在Web方法中使用以下代码:
In WS.java
, I am using the following code in a Web Method:
InputStream fstream = this.getClass().getResourceAsStream("abc.txt");
但它始终返回null。我需要从该文件中读取,并且我读到如果您将文件放在 WEB-INF
中,您可以使用 getResourceAsStream $访问它们c $ c>,但该方法始终返回
null
。
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
.
关于我可能在做什么的任何想法错误?
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 :/
推荐答案
据我所知,文件必须在<$的文件夹中c $ c>'this'类驻留,即不在 WEB-INF / classes
中,但嵌套得更深(除非您在默认包中写入) ):
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):
net/domain/pkg1/MyClass.java
net/domain/pkg1/abc.txt
将文件放入java源代码应该可以工作,编译器将该文件与类文件一起复制。
Putting the file in to your java sources should work, compiler copies that file together with class files.
这篇关于getResourceAsStream()始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!