我有一个扩展 TagSupport 的类文件,在这里我想读取一个属性文件,但它不起作用。我认为 getResourceAsStream 返回 null。
我正在 bith 系统上使用分解的 war 文件。
代码:
public class MyTag extends TagSupport {
private int getUsers() { ServletContext servletContext = pageContext.getServletContext(); InputStream in = pageContext.getServletContext().getResourceAsStream("/WEB-INF/app.properties"); Properties properties = new Properties(); properties.load(in); int users= Integer.parseInt(properties.getProperty("users")); return users; }
}
此代码可以在我的电脑上运行,但不能在测试系统上运行。我收到以下异常:
java.lang.NullPointerException ERROR [STDERR] at java.util.Properties$LineReader.readLine(Properties.java:418) ERROR [STDERR] at java.util.Properties.load0(Properties.java:337) ERROR [STDERR] at java.util.Properties.load(Properties.java:325)
最佳答案
为什么您的 app.properties
位于 /WEB-INF
中?如果是 /WEB-INF/classes
,您只需调用 getResourceAsStream("/app.properties")
即可。这是因为 getResourceAsStream(...)
的参数相对于类路径进行解析。
关于java - getResourceAsStream 使用 TagSupport 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17833600/