如何从JSF / Webflow应用程序提供二进制内容? [英] How can I serve binary content from a JSF/Webflow app?

查看:151
本文介绍了如何从JSF / Webflow应用程序提供二进制内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要提供二进制内容(图像)的JSF 1.2 / Spring Web flow 2.0.7应用程序。此内容作为Base64编码的字符串从Web服务(以及其他一些数据)获取,并与bean的其余数据一起在bean中结束。如何将图像显示在我的网页上?

I have a JSF 1.2/Spring Web flow 2.0.7 app that need to serve binary content (an image). This content is fetched from a web service (along with some other data) as a Base64-encoded string, and ends up in the bean together with the rest of the data. How can I get the image to show on my web page?

注意:不,没有办法让网络服务直接传输数据,甚至无法获取没有所有其他东西的Web服务中的二进制数据。

Note: No, there is no way to make the web service stream the data directly, or even to get the binary data out of the web service without all the other stuff.

推荐答案

您希望在< h:graphicImage> 组件,对吗?理论上,您可以使用 数据 URI格式

You want to end up with that image in a <h:graphicImage> component, right? In theory, you could use the data URI format for this.

<h:graphicImage value="data:image/png;base64,#{bean.base64Image}" />

但是,您遇到的问题是它无法在所有当前浏览器中运行。例如,MSIE将数据 URI的长度限制为32KB。

However, you've the problem that it doesn't work across all of the current browsers. MSIE for example limits the length of data URI to 32KB.

如果这些图像通常较大或者你'我也想支持过时的浏览器,那么你目前最好的选择就是让它在所有之后指向一个完整的URL

If those images are in general larger or you'd like to support outdated browsers as well, then your currently best bet is really to let it point to a fullworthy URL after all.

<h:graphicImage value="images/filename.png" />

有两种方法可以让它发挥作用:

There are two ways to get this to work:


  1. 暂时将图像写入公共webcontent并以通常的方式引用它。

  1. Write the image temporarily to public webcontent and reference it the usual way.

this.uniqueImageFileName = getOrGenerateItSomehow();
byte[] imageContent = convertBase64ToByteArraySomehow();
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ServletContext sc = (ServletContext) ec.getContext();
File image = new File(sc.getRealPath("/images"), uniqueImageFileName);
// Write byte[] to FileOutputStream on that file the usual way (and close!)

并按如下方式使用:

<h:graphicImage value="images/#{bean.uniqueImageFileName}" />

但是,只有在扩展WAR并且您具有磁盘文件系统的写权限时,此功能才有效。您还需要考虑清理。 HttpSessionListener 可能对此有所帮助。

This however only works if the WAR is expanded and you have write rights to the disk file system. You also need to take cleanup into account. A HttpSessionListener may be helpful in this.

将二进制数据存储在会话中并具有 HttpServlet 来提供它。假设你的bean是请求作用域:

Store the binary data in session and have a HttpServlet to serve it up. Assuming that your bean is request scoped:

this.uniqueImageFileName = getOrGenerateItSomehow();
byte[] imageContent = convertBase64ToByteArraySomehow();
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.getSessionMap().put(uniqueImageFileName, imageContent);

,视图如下所示:

<h:graphicImage value="images/#{bean.uniqueImageFileName}" />

创建一个 HttpServlet ,它映射到一个 url-pattern of / images / * 并且在 doGet()中执行以下操作方法:

Create a HttpServlet which is mapped on an url-pattern of /images/* and does like the following in the doGet() method:

String uniqueImageFileName = request.getPathInfo().substring(1);
byte[] imageContent = (byte[]) request.getSession().getAttribute(uniqueImageFileName);
response.setContentType("image/png"); // Assuming it's always PNG.
response.setContentLength(imageContent.length);
// Write byte[] to response.getOutputStream() the usual way. 


这篇关于如何从JSF / Webflow应用程序提供二进制内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆