java getSystemResourceAsStream() 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2522235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getSystemResourceAsStream() returns null
提问by Hitesh Solanki
Hiii... I want to get the content of properties file into InputStream class object using getSystemResourceAsStream(). I have built the sample code. It works well using main() method,but when i deploy the project and run on the server, properties file path cannot obtained ... so inputstream object store null value.
Hiii... 我想使用 getSystemResourceAsStream() 将属性文件的内容放入 InputStream 类对象中。我已经构建了示例代码。它使用 main() 方法运行良好,但是当我部署项目并在服务器上运行时,无法获取属性文件路径......因此输入流对象存储空值。
Sample code is here..
示例代码在这里..
public class ReadPropertyFromFile {
public static Logger logger = Logger.getLogger(ReadPropertyFromFile.class);
public static String readProperty(String fileName, String propertyName) {
String value = null;
try {
//fileName = "api.properties";
//propertyName = "api_loginid";
System.out.println("11111111...In the read proprty file.....");
// ClassLoader loader = ClassLoader.getSystemClassLoader();
InputStream inStream = ClassLoader.getSystemResourceAsStream(fileName);
System.out.println("In the read proprty file.....");
System.out.println("File Name :" + fileName);
System.out.println("instream = "+inStream);
Properties prop = new Properties();
try {
prop.load(inStream);
value = prop.getProperty(propertyName);
} catch (Exception e) {
logger.warn("Error occured while reading property " + propertyName + " = ", e);
return null;
}
} catch (Exception e) {
System.out.println("Exception = " + e);
}
return value;
}
public static void main(String args[]) {
System.out.println("prop value = " + ReadPropertyFromFile.readProperty("api.properties", "api_loginid"));
}
}
回答by BalusC
i deploy the project and run on the server,
我部署项目并在服务器上运行,
This sounds like a JSP/Servlet webapplication. In that case, you need to use the ClassLoaderwhich is obtained as follows:
这听起来像一个 JSP/Servlet web 应用程序。在这种情况下,您需要使用ClassLoader按如下方式获得的 :
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
This one has access to the allclasspath paths tied to the webapplication in question and you're not anymore dependent on which parent classloader (a webapp has more than one!) has loaded your class.
这个可以访问与所讨论的 web 应用程序相关联的所有类路径路径,并且您不再依赖于哪个父类加载器(一个 web 应用程序有多个!)加载了您的类。
Then, on this classloader, you need to just call getResourceAsStream()to get a classpath resource as stream, not the getSystemResourceAsStream()which is dependent on how the webapplication is started. You don't want to be dependent on that as well since you have no control over it at external hosting:
然后,在这个类加载器上,您只需调用getResourceAsStream()以获取类路径资源作为流,而不是getSystemResourceAsStream()依赖于 web 应用程序的启动方式。您也不想依赖它,因为您无法在外部托管上控制它:
InputStream input = classLoader.getResourceAsStream("filename.extension");
This is finally more robust than your initial getSystemResourceAsStream()approach and the Class#getResourceAsStream()as suggested by others.
这最终比您最初的getSystemResourceAsStream()方法和Class#getResourceAsStream()其他人建议的方法更强大。
回答by bruno conde
The SystemClassLoaderloads resources from java.class.pathwitch maps to the system variable CLASSPATH. In your local application, you probably have the resource your trying to load configured in java.class.pathvariable. In the server, it's another story because most probably the server loads your resources from another class loader.
在SystemClassLoader从加载资源java.class.path巫映射到系统变量CLASSPATH。在您的本地应用程序中,您可能将尝试加载的资源配置为java.class.path变量。在服务器中,这是另一回事,因为服务器很可能从另一个类加载器加载您的资源。
Try using the ClassLoaderthat loaded class using the correct path:
尝试使用ClassLoader正确的路径使用加载的类:
getClass().getResourceAsStream(fileName);
This articlemight also be useful.
这篇文章也可能有用。
回答by Chandra Sekar
Try using getResourceAsStream()instead of getSystemResourceAsStream().
尝试使用getResourceAsStream()而不是getSystemResourceAsStream().

