无法从Websphere 8.5服务器的web-inf文件夹加载属性文件
问题描述:
我想从Websphere 8.5上运行的Web应用程序中的WEb-INF文件夹加载属性文件。我使用下面的代码从位置无法从Websphere 8.5服务器的web-inf文件夹加载属性文件
public class Init {
private final String WEB_INF_DIR_NAME="WEB-INF";
private String web_inf_path;
private final Properties APP_PROPERTIES =null;
InputStream inputStream = null;
public String getWebInfPath() throws IOException {
if (web_inf_path == null) {
web_inf_path = URLDecoder.decode(Init.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF8");
web_inf_path=web_inf_path.substring(0,web_inf_path.lastIndexOf(WEB_INF_DIR_NAME)+WEB_INF_DIR_NAME.length()).substring(1);
}
inputStream = Init.class.getResourceAsStream("/config/localhost/accountservice.properties");
// inputStream = this.getClass().getClassLoader().getResourceAsStream("/config/localhost/accountservice.properties");
if (inputStream != null) {
APP_PROPERTIES.load(inputStream);
}
System.out.println(APP_PROPERTIES.getProperty(AccountServiceDataAccessConstants.INET_LIBRARY_NAME)); // Here i am getting NULL
return web_inf_path;
}
}
我一直在使用servlet上下文也尝试加载该文件,但它也给我NULL。我尝试了所有可能的方法来解决它,但不幸的是我无法做到这一点。我也给我的文件夹结构。
请原谅我,如果这是一个愚蠢的问题,但我没有真正得到任何关于它的想法。
答
通常,WebContent中的所有内容都放在WAR文件的根目录中。因此,而不是
inputStream = Init.class.getResourceAsStream("/config/localhost/accountservice.properties");
这将是
inputStream = Init.class.getResourceAsStream("/WEB-INF/config/localhost/accountservice.properties");
战争的根源有WEB-INF中,然后就可以进入到您的文件夹结构正常。
据我所知,属性文件位于resources文件夹中。如果您使用的是Maven,您可以在您的pom.xml中定义资源文件夹。通常,它们位于'/ src/main/resources'中。从那里,你可以像你这样访问它:'MyClass.class.getResourcesAsStream(...)' – Al1