无法从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。我尝试了所有可能的方法来解决它,但不幸的是我无法做到这一点。我也给我的文件夹结构。

enter image description here

请原谅我,如果这是一个愚蠢的问题,但我没有真正得到任何关于它的想法。

+0

据我所知,属性文件位于resources文件夹中。如果您使用的是Maven,您可以在您的pom.xml中定义资源文件夹。通常,它们位于'/ src/main/resources'中。从那里,你可以像你这样访问它:'MyClass.class.getResourcesAsStream(...)' – Al1

通常,WebContent中的所有内容都放在WAR文件的根目录中。因此,而不是

inputStream = Init.class.getResourceAsStream("/config/localhost/accountservice.properties"); 

这将是

inputStream = Init.class.getResourceAsStream("/WEB-INF/config/localhost/accountservice.properties"); 

战争的根源有WEB-INF中,然后就可以进入到您的文件夹结构正常。

+0

为了扩展这个... WEB-INF目录本身通常不是Web模块类路径的一部分。默认情况下,您将在WEB-INF/lib(但不是WEB-INF/lib目录本身)中获得WAR根目录,WEB-INF/classes目录和jar文件。如果您不想在getResource参数中使用WEB-INF,则可以将“config”目录结构移至父目录或向下移至WEB-INF /类。 – Jarid

+0

@lwestby获取相同的空指针异常 – Mandrek

+0

提取并检查您生成的WAR文件。你期望它的文件是? – lwestby