从 tomcat webapps 文件夹中读取属性文件 [英] Reading Property file from tomcat webapps folder

查看:46
本文介绍了从 tomcat webapps 文件夹中读取属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tomcat 的 webaaps 文件夹中放置了一个属性文件,但无法访问它.我的代码是

I have placed a property file in the webaaps folder of tomcat but not able to access it. My code is

InputStream is = ClassLoader.getSystemResourceAsStream("database.properties");
if(is == null){
    is = ClassLoader.getSystemClassLoader().getResourceAsStream("database.properties");
}
if(is == null){
    is = ClassLoader.getSystemClassLoader().getResourceAsStream("/database.properties");
}
if(is == null){
    is = ClassLoader.getSystemClassLoader().getResourceAsStream("/webapps/database.properties");
}
if(is == null){
    is = ClassLoader.getSystemClassLoader().getResourceAsStream("webapps/database.properties");
}

仍然,我将输入流设为空.

Still , I am getting input stream as null.

推荐答案

getSystemResourceAsStream 方法返回 null 表示在类路径上找不到资源指定的位置.这是有道理的,因为 $TOMCAT_HOME/webapps 不在类路径上.

The getSystemResourceAsStream method returning null means the resource was not found on the classpath at the specified location. And this makes sense since $TOMCAT_HOME/webapps isn't on the classpath.

因此,要么将该文件放在应用程序类路径上的目录中(如果您想将其放在战争之外,则必须将其放在 $CATALINA_HOME/lib - 带有所有隐含的后果)或更改您的方法(使用 JNDI 存储属性、使用绝对路径等).

So either put that file in a directory that is on the classpath of your application (and if you want to have it outside the war, then you'll have to put it in $CATALINA_HOME/lib - with all the implied consequences) or change your approach (use JNDI to store the properties, use an absolute path, etc).

  • Apache Tomcat 6.0 文档

    这篇关于从 tomcat webapps 文件夹中读取属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆