我在 Tomcat 中部署了我的 reSTLet.Component,在此,我读取了我的应用程序的配置文件:
this.properties.load(new FileInputStream("config/conf.properties));
我有一个java.io.FileNotFoundException
我的 webapp 结构是:
/mi-webapp
/config
conf.properties
/WEB-INF
/classes
/lib
web.xml
如何获取reSTLet中的真实路径?我阅读了有关 ServletContext.getRealPath()
的内容,但这是一种不好的做法,您有何建议?
最佳答案
将配置目录移动到 WEB-INF/classes
中并改用此代码:
URL url = this.class.getClassLoader().getResource("config/conf.properties");
try {
this.properties.load(url.openStream());
} catch (IOException e) {
System.out.println("Couldn't open/load properties file");
}
通过这种方式,您可以从应用程序范围内的类路径加载文件,并且不会导致任何安全漏洞。
关于tomcat - Tomcat 中使用 ReSTLet 的路径目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11795387/