简体   繁体   English

如何从WAR中的classes目录中读取文件?

[英]How can I read file from classes directory in my WAR?

I need to read text file from the classpath in Java WAR application. 我需要从Java WAR应用程序中的类路径中读取文本文件。 How can I read it as InputStream. 我怎么能把它读作InputStream。 File is located in /WEB-INF/classes/ folder, but when I use following code, it just returns null. 文件位于/ WEB-INF / classes /文件夹中,但是当我使用以下代码时,它只返回null。

InputStream input = servletContext.getClass().getClassLoader().getResourceAsStream("my_filename.txt");

Prefix it with a forward slash to denote the root of the classpath: 使用正斜杠对其进行前缀以表示类路径的根:

getResourceAsStream("/my_filename.txt")

Alternatively, you can use the serlvetContext.getResourceAsStream(..) which looks for resources relative to the context root. 或者,您可以使用serlvetContext.getResourceAsStream(..)查找相对于上下文根的资源。 So classes would be /WEB-INF/classes . 类是/WEB-INF/classes

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

相关问题 Jetty不会将我的WAR文件提取到temp目录中。 如何阻止其提取或确保将其提取到temp目录? - Jetty is not extracting my WAR file into temp directory. How can I stop it from extracting or make sure it extracts to the temp directory? 我可以简单地将war文件解压缩并复制到我的服务器目录中吗? - Can I simply unzip war file and copy it in my server directory? 我如何在war中读取xml文件? -专家 - How can I read xml file inside war? - maven 我可以将文本文件从类中读取到对象中,然后在JAVA ADF中以VO格式读取吗? - Can I read a text file into objects from my classes and read in VO in JAVA ADF? 如何从外部项目目录读取资源文件? - How can I read resources file from outside project directory? 如何在 NetBeans 中创建我的项目的 war 文件? - How can I create a war file of my project in NetBeans? 如何从我的战争中访问文本文件 - How do I access a text file from within my war 如何从耳朵/战争/罐子中读取文件? - How to read file from ear/war/jar? 如何告诉JBoss 4.2.3.GA首先在我的war文件lib目录中加载jar? - How do I tell JBoss 4.2.3.GA to load jars in my war file lib directory first? 如何在另一个文件、不同目录中使用 jar 文件中的类? - How can I use the classes from a jar file in another files, different directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM