简体   繁体   English

如何从JSF / Webflow应用程序提供二进制内容?

[英]How can I serve binary content from a JSF/Webflow app?

I have a JSF 1.2/Spring Web flow 2.0.7 app that need to serve binary content (an image). 我有一个需要提供二进制内容(图像)的JSF 1.2 / Spring Web flow 2.0.7应用程序。 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. 此内容作为Base64编码的字符串从Web服务(以及其他一些数据)中获取,并最终在bean中与其余数据一起结束。 How can I get the image to show on my web page? 如何在我的网页上显示图像?

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. 注意:不,没有办法让Web服务直接传输数据,甚至没有其他所有东西从Web服务中获取二进制数据。

You want to end up with that image in a <h:graphicImage> component, right? 你想在<h:graphicImage>组件中得到那个图像,对吧? In theory, you could use the data URI format for this. 理论上,您可以使用data URI格式

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

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. 例如,MSIE将data URI的长度限制为32KB。

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 . 如果这些图像是在一般较大,或者你想支持过时的浏览器为好,那么你现在最好的办法就是真正让它指向fullworthy URL 毕竟

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

There are two ways to get this to work: 有两种方法可以使其工作:

  1. Write the image temporarily to public webcontent and reference it the usual way. 暂时将图像写入公共webcontent并以通常的方式引用它。

     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!) 

    And use it as follows: 并使用如下:

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

    This however only works if the WAR is expanded and you have write rights to the disk file system. 但是,只有在扩展WAR且您具有磁盘文件系统的写权限时,此方法才有效。 You also need to take cleanup into account. 您还需要考虑清理。 A HttpSessionListener may be helpful in this. HttpSessionListener可能对此有所帮助。

  2. Store the binary data in session and have a HttpServlet to serve it up. 将二进制数据存储在会话中并使用HttpServlet来提供服务。 Assuming that your bean is request scoped: 假设您的bean是请求作用域:

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

    and the view look like this: 并且视图看起来像这样:

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

    Create a HttpServlet which is mapped on an url-pattern of /images/* and does like the following in the doGet() method: 创建一个映射在/images/*url-pattern上的HttpServlet ,并在doGet()方法中执行以下操作:

     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. 
  1. Obtain the response object (in jsf - FacesContext.getCurrentInstance().getExternalContext().getResponse() , and cast to HttpServletResponse ) or the OutputStream (JSF 2.0, the last method call above would be getResponseOutputStream() ) 获取响应对象(在jsf - FacesContext.getCurrentInstance().getExternalContext().getResponse() ,并转换为HttpServletResponse )或the OutputStream (JSF 2.0,上面的最后一个方法调用是getResponseOutputStream()

  2. use the write methods on the response's OutputStream 在响应的OutputStream上使用write方法

  3. set the proper Content-Type header 设置正确的Content-Type标头

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM