简体   繁体   English

如何将inputstream转换为java中tomcat中web-inf文件夹下的配置文件?

[英]how to get the inputstream to configuration file lying under web-inf folder in tomcat in java?

i want to get the inputstream to configuration file lying under webinf folder in tomcat in java ?i want the configuration file as inputstream lying under web-inf folder in tomcat in java . 我想将inputstream配置文件放在java的tomcat中的webinf文件夹下?我想将配置文件作为inputstream配置在java的tomcat中的web-inf文件夹下。 I tried below code 我尝试下面的代码

InputStream xwikicfgis = FIPSSOAuthServiceImpl.class.getClassLoader().getResourceAsStream("config.cfg");

but its trying to search the file under web-inf/classes folder but my file lies directly under web-inf folder. 但是它试图搜索web-inf / classes文件夹下的文件,但是我的文件直接位于web-inf文件夹下。 i am not getting how to point the path to web-inf instead of web-inf/classes folder? 我没有得到如何指向web-inf而不是web-inf / classes文件夹的路径?

您可以从任何servlet使用servletConfig.getServletContext()。getResourceAsStream(“ / WEB-INF / filename”)来获取它。

use javax.servlet.ServletContext.getRealPath(String) 使用javax.servlet.ServletContext.getRealPath(String)

the call 电话

String webappBase = getServletContext().getRealPath("/");

will point you to the doc base of your webapp, this is the parent of WEB-INF 会将您指向您的Web应用程序的文档库,这是WEB-INF的父级

after that you can use 之后,您可以使用

InputStream is = new FileInputStream(webappBase + File.separator + "WEB-INF" + 
                                            File.separator + "config.cfg");

to get your input stream 获得您的输入流

Or you can use: 或者您可以使用:

System.getenv("CATALINA_HOME") + "/webapps/your project name/foo.cfg

Tomcat can't work without CATALINA_HOME, so we can use it without any additional config file or something else and you don't need to take with you huge ServletContext. 如果没有CATALINA_HOME,Tomcat将无法工作,因此我们可以在没有任何其他配置文件或其他任何内容的情况下使用它,并且您无需携带庞大的ServletContext。

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

相关问题 如何在Java类中的WEB-INF文件夹下访问文件 - How to access a file under WEB-INF folder in java class 在WAR文件的WEB-INF文件夹下打包Java类 - Package Java classes under WEB-INF folder in WAR file 如何从远程服务器为位于apache-tomcat WEB-INF / lib文件夹下的jar文件制作下载URL? - How to make a download url for the jar file located under apache-tomcat WEB-INF/lib folder from remote server? 如何获取WEB-INF / class文件夹下的资源路径(java ee web动态项目) - How can I get path of resource under WEB-INF/class folder (java ee web dynamic project) 如何在tomcat或jetty下更改Java webapp'WEB-INF路径'约定 - How to change the Java webapp 'WEB-INF path' convention under tomcat or jetty 如何访问WEB-INF下文件夹内的jsp页面? - How to access a jsp page inside a folder under WEB-INF? 如何从Spring Java配置@Configuration类从WEB-INF获取资源? - How to get resources from WEB-INF from Spring java configuration @Configuration class? 在Eclipse中访问Java项目中WEB-INF下的ftl文件 - Accessing an ftl file under WEB-INF in a Java Project in eclipse 如何访问 WEB-INF 文件夹之外的 jdbc 属性文件(用于 Spring Security 配置)? - How to access jdbc properties file (for Spring Security configuration) which is outside WEB-INF folder? 使用init读取WEB-INF文件夹Java中的属性文件 - Read Properties file in WEB-INF folder java using init
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM