[英]How to access a file under WEB-INF folder in java class
I have a plain java class in a web application and want to read a configuration file under WEB-INF
folder. 我在Web应用程序中有一个普通的Java类,想读取WEB-INF
文件夹下的配置文件。 I know the way to access the file if its in the classpath ( WEB-INF/classes
folder). 我知道访问文件的方式(如果它在类路径中)( WEB-INF/classes
文件夹)。 Since WEB-INF/classes
folder is meant for .class
files, I want to keep my configuration file under WEB-INF
folder only. 由于WEB-INF/classes
文件夹用于.class
文件,因此我只想将配置文件保留在WEB-INF
文件夹下。
Can anyone tell me how I can access it from my java class? 谁能告诉我如何从我的java类访问它?
ServletContext.getResourceAsStream() will load a file from a given path relative to the root of the WAR file. ServletContext.getResourceAsStream()将从相对于WAR文件根目录的给定路径加载文件。 Something like: 就像是:
ServletContext ctx;
InputStream configStream = ctx.getResourceAsStream("/WEB-INF/config.properties");
The major issue here is that you need access to the servlet context to be able to do this. 这里的主要问题是您需要访问servlet上下文才能执行此操作。 You have that in a servlet or a filter, but not in a non-web component further back in the application. 您可以在servlet或过滤器中找到它,但在应用程序后面的非Web组件中则不能。 You have a few options: 您有几种选择:
You can get the absolute path of servlet using getRealPath()
method of ServletContext
and then append WEB-INF
to the path you get. 您可以使用ServletContext
getRealPath()
方法获取Servlet的绝对路径,然后将WEB-INF
附加到获取的路径。 I think this is very basic there may be some other answers as well. 我认为这是非常基本的,可能还会有其他答案。
"new FIleInputStream( Utility.class.getClassLoader().getResource(keyFileName).getPath() )" worked for me. “ new FIleInputStream(Utility.class.getClassLoader()。getResource(keyFileName).getPath())”对我有用。
Here "Utility" is my class name where the code is calling this line , "keyFileName" is the file i need to open 这里的“ Utility”是我的类名,代码正在调用此行,“ keyFileName”是我需要打开的文件
hey you all care for context related file loading like application context , web.xml ,config and property file
Here is how to load a java file any kind of file under WEB-INF
but it stored on another stucture like a sub folder reportFile
the your file or sub folder again report01
-- 这是如何在WEB-INF
下加载任何类型的Java文件的方法,但是它存储在另一个结构中,例如子文件夹reportFile
再次将文件或子文件夹report01
fullpath is = /WEB-INF/reportFile/report01/report.xml
,i have tried many possibilities to load and read this xml file ...none of the above worked for me but , here is the trick for future use... fullpath is = /WEB-INF/reportFile/report01/report.xml
,我已经尝试了许多加载和读取此xml文件的可能性...以上都不适合我,但这是将来使用的窍门...
In Action or inservice class you know interface implementation class
no imports
that is good part also. In Action or inservice class you know interface implementation class
没有imports
,这也很重要。
File myClass = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getFile());
System.out.println("Finding calss path first then remove classes from the path " + myClass.getCanonicalPath().replaceFirst("classes", "")+"reportFIle/report01/reports.xml")
classes
from the above and add your specific path 2.通过从上面删除classes
来加载路径并添加您的特定路径 File f = new File(myClass.getCanonicalPath().replaceFirst("classes", "")+"reportFile/report01/reports.xml")
you can even parse it using xml parser or do anything 您甚至可以使用xml解析器解析它或执行任何操作
document = docBuilder.parse(new File(myClass.getCanonicalPath().replaceFirst("classes", "")+"reportFile/report01/reports.xml"));
Cheers!! 干杯!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.