如何获取公共网页内容资源的InputStream? [英] How to obtain an InputStream of a public webcontent resource?
问题描述
我在我的 WAR 中打包了一组图像,我使用
在
中描绘了它们.图像位于 /resources/icons
文件夹中.我希望能够选择一个图像并在提交时将此图像的副本保存到磁盘.
这是怎么做到的?如何获得此图像的引用(InputStream
或其他)?
鉴于此文件夹结构,
<前>你的项目|-- 源代码|`--com|`-- 示例|`--BackingBean.java|-- 网页内容||-- 元信息||-- 网络信息||-- 资源||`-- 图标||`--foo.png|`-- foo.xhtml:您可以通过 ExternalContext#getResourceAsStream()
使用 webcontent-relative 路径:
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();InputStream input = externalContext.getResourceAsStream("/resources/icons/foo.png");//...
或者通过 Resource#getInputStream()
其中 Resource
是从 ResourceHandler#createResource()
需要一个 /resources
-相对路径:
ResourceHandler resourceHandler = FacesContext.getCurrentInstance().getApplication().getResourceHandler();InputStream input = resourceHandler.createResource("icons/foo.png").getInputStream();//...
至于选择图像并传递其路径,只需执行如下操作:
<f:ajax event="click" listener="#{bean.setImage(component.name)}"/></h:graphicImage><h:graphicImage name="icons/bar.png"><f:ajax event="click" listener="#{bean.setImage(component.name)}"/></h:graphicImage><h:commandButton value="submit" action="#{bean.saveImage}"/>
另见:
- getResourceAsStream() 与 FileInputStream
- 在基于 servlet 的应用程序中,在哪里放置以及如何读取配置资源文件?
- Maven 和 JSF webapp 结构,JSF资源到底放在哪里
I have a collection of images packaged in my WAR and I depict them in a <p:dataGrid>
using <p:graphicImage>
. The images are located in the /resources/icons
folder. I want to be able to select an image and save a copy of this image to disk on submit.
How can this be done? How can I get a reference (InputStream
or whatever) to this image?
Given this folder structure,
YourProject |-- src | `-- com | `-- example | `-- BackingBean.java |-- WebContent | |-- META-INF | |-- WEB-INF | |-- resources | | `-- icons | | `-- foo.png | `-- foo.xhtml :
You can get it by either ExternalContext#getResourceAsStream()
which takes webcontent-relative path:
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
InputStream input = externalContext.getResourceAsStream("/resources/icons/foo.png");
// ...
Or by Resource#getInputStream()
wherein Resource
is obtained from ResourceHandler#createResource()
which takes a /resources
-relative path:
ResourceHandler resourceHandler = FacesContext.getCurrentInstance().getApplication().getResourceHandler();
InputStream input = resourceHandler.createResource("icons/foo.png").getInputStream();
// ...
As to selecting the image and passing its path around, just do something like as follows:
<h:graphicImage name="icons/foo.png">
<f:ajax event="click" listener="#{bean.setImage(component.name)}" />
</h:graphicImage>
<h:graphicImage name="icons/bar.png">
<f:ajax event="click" listener="#{bean.setImage(component.name)}" />
</h:graphicImage>
<h:commandButton value="submit" action="#{bean.saveImage}" />
See also:
- getResourceAsStream() vs FileInputStream
- Where to place and how to read configuration resource files in servlet based application?
- Maven and JSF webapp structure, where exactly to put JSF resources
这篇关于如何获取公共网页内容资源的InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!