一般的應用都會有一個或幾個配置文件放在web-inf下面,getRealPaht返回null是讀配置文件有很大困難。解決方法是使用getServletContext().getResourceAsStream.示例如下:
InputStream is = getServletContext().getResourceAsStream("/WEB-INF/config.properties");
Properties props = new Properties();
try {
props.load(is);
} catch (IOException e) {
System.err.println("Load configuration failed");
}
PropertyConfigurator.configure(props);
上面方法並沒有解決根本問題,根本問題就是得到RealPath,可以使用下面方法得到RealPath:
try {
URL u = getServletContext().getResource("/");
String weblogic_path=u.getPath();
} catch (MalformedURLException e) {
e.printStackTrace();
}
這個語句得到的RealPath可能不是最終的答案(一般來說前面多了一個/),需要你根據實際情況去加工一下。
至此問題全部解決。