简体   繁体   English

ExternalContext#getResourceAsStream()返回null,将资源文件放置在哪里?

[英]ExternalContext#getResourceAsStream() returns null, where to place the resource file?

I'm trying to obtain a PNG file as InputStream in my managed bean as below: 我正在尝试在托管bean中获取PNG文件作为InputStream ,如下所示:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
InputStream input = externalContext.getResourceAsStream("/myFile.png");
// input is null.

However, the InputStream is always null. 但是, InputStream始终为null。 How is this caused and how can I solve it? 这是怎么引起的,我该如何解决?

Apparently you placed the resource in physically the wrong location. 显然您将资源放置在错误的位置。

The ExternalContext#getResourceAsStream() , which delegates in case of servlet containers under the covers to ServletContext#getResoruceAsStream() , has its root in the web content of the WAR (the parent folder of /WEB-INF and /META-INF folders, thus the files therein are also available this way), and the /META-INF/resources folder of all JARs in /WEB-INF/lib . ExternalContext#getResourceAsStream()在Servlet容器的情况下将其委托给ServletContext#getResoruceAsStream() ,其根位于WAR的Web内容中( /WEB-INF/META-INF文件夹的父文件夹,因此,其中的文件也可以通过这种方式获得),以及/WEB-INF/lib中所有JAR的/META-INF/resources文件夹。 In case of a JSF web application it are usually XHTML, CSS, JavaScript and image files. 对于JSF Web应用程序,通常是XHTML,CSS,JavaScript和图像文件。

In other words, it returns web resources. 换句话说,它返回Web资源。 It doesn't return a disk file system resource, for that you need new FileInputStream() instead. 它不会返回磁盘文件系统资源,因为您需要new FileInputStream() It also doesn't return a classpath resource, for that you need ClassLoader#getResourceAsStream() instead. 它还不会返回类路径资源,因为您需要ClassLoader#getResourceAsStream() The classpath has its root in ao /WEB-INF/classes , all JARs in /WEB-INF/lib , and some VM/server-configured folders depending on the runtime environment. 类路径的根源于/WEB-INF/classes ,所有JAR位于/WEB-INF/lib ,并且某些VM / server配置的文件夹取决于运行时环境。

In an usual web content file structure, the resource file has to be placed exactly here in order to obtain it the desired way: 在通常的Web内容文件结构中,必须将资源文件准确放置在此处,以便以所需的方式获取它:

WebContent
 |-- META-INF
 |-- WEB-INF
 |    |-- faces-config.xml
 |    `-- web.xml
 |-- myFile.png    <-- Here.
 :

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 externalContext.getSession(false)返回null - externalContext.getSession(false) returns null ExternalContext的Request对象上的getQueryString在WebSphere上返回null - getQueryString on ExternalContext's Request object returns null on WebSphere 在ExternalContext#getSessionMap()中访问@SessionScoped @ManagedBean类将返回null - Accessing @SessionScoped @ManagedBean class in ExternalContext#getSessionMap() returns null welcome-file index.xhtml提供“在ExternalContext中找不到”作为资源 - welcome-file index.xhtml gives Not Found in ExternalContext as a Resource 404 /index.xhtml在ExternalContext中找不到作为资源 - 404 /index.xhtml Not Found in ExternalContext as a Resource 错误:在ExternalContext中找不到/login.xhtml作为资源 - Error: /login.xhtml Not Found in ExternalContext as a Resource 在ExternalContext中找不到/index.xhtml作为资源 - /index.xhtml Not Found in ExternalContext as a Resource IntelliJ项目错误“在外部上下文中找不到/hello.xhtml作为资源” - IntelliJ project errors “/hello.xhtml Not Found in ExternalContext as a Resource” 错误:在ExternalContext中找不到/JavaServerFaces/default.xhtml作为资源 - Error: /JavaServerFaces/default.xhtml Not Found in ExternalContext as a Resource 使用ExternalContext从HttpServletRequest中检索时,HttpSession为null(随机) - HttpSession is null (randomly) when retrieved from HttpServletRequest using ExternalContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM