[英]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.