简体   繁体   English

如何在Java类中的WEB-INF文件夹下访问文件

[英]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: 您有几种选择:

  • Make the servlet context available from the web layer to the service layer, via an application-scoped variable, or injection, or some other way 通过应用程序范围的变量,注入或其他方式,使Servlet上下文可从Web层到服务层可用
  • Put the resource-loading code in the web layer, and expose that to the service layer 将资源加载代码放入Web层,并将其公开给服务层
  • Load the configuration in the web layer, and pass it on to the service layer 将配置加载到Web层,然后将其传递到服务层

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 ,这也很重要。

declare your file object 声明文件对象

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")

2.Load the path by removing classes from the above and add your specific path 2.通过从上面删除classes来加载路径并添加您的特定路径

File f = new File(myClass.getCanonicalPath().replaceFirst("classes", "")+"reportFile/report01/reports.xml")

Then 然后

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.

相关问题 在WAR文件的WEB-INF文件夹下打包Java类 - Package Java classes under WEB-INF folder in WAR file 如何访问WEB-INF下文件夹内的jsp页面? - How to access a jsp page inside a folder under WEB-INF? 如何将inputstream转换为java中tomcat中web-inf文件夹下的配置文件? - how to get the inputstream to configuration file lying under web-inf folder in tomcat in java? 如何从引用java项目访问静态资源(WEB-INF)文件夹中的文件? - How to access file in the static resources(WEB-INF) folder from the referencing java project? Java - 如何在没有 servlet 上下文的情况下访问 WEB-INF 文件夹中的文件 - Java - How to access a file in the WEB-INF folder WITHOUT servlet context 如何获取WEB-INF / class文件夹下的资源路径(java ee web动态项目) - How can I get path of resource under WEB-INF/class folder (java ee web dynamic project) 如何在java Web应用程序中访问WEB-INF / META-INF中的文件? - How can I access a file in WEB-INF/META-INF in a java web app? 如何将.class下的web-inf / classs打包为jar并将其放入web-inf / lib目录 - how to package the .class under the web-inf/classes as a jar and put it to the web-inf/lib dir 在Eclipse中访问Java项目中WEB-INF下的ftl文件 - Accessing an ftl file under WEB-INF in a Java Project in eclipse 如何在web-inf / class下的类名称空间中创建/写入文件 - How to create/write to file in class namespace under web-inf/class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM