如何使用af:commandMenuItem从JSF支持bean提供文件下载?

时间:2015-06-09 14:33:10

标签: jsf jsf-2 download oracle-adf backing-beans

这是以下问题的精确副本: How to provide a file download from a JSF backing bean?

除了我尝试使用不同的面孔,所描述的方法也不起作用。

注意:我不能使用

使用时 - 文件下载正常。

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
      xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">

我正在尝试使用并且页面只是刷新,我不知道它是否为请求启用了ajax但是答案中描述的方法不起作用。

这是我的对象代码:

<af:commandMenuItem text="Test" id="cmi2389"
                rendered="true"
                actionListener="#{pageFlowScope.InkassPorReportBean.dlw}"
                partialSubmit="false" immediate="true">
</af:commandMenuItem>

如果我将内容长度设置为文件大小,也会出现以下错误,如果未设置响应大小,则不会发生这种情况:

<Jun 9, 2015 7:37:14 PM MSK> <Error> <HTTP> <BEA-101083> <Connection failure.
java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '0' bytes instead of stated: '-1' bytes.
    at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:463)
> 

<Jun 9, 2015 7:37:14 PM MSK> <Error> <HTTP> <BEA-101104> <Servlet execution in servlet context "ServletContext@1243894494[app:sxdocs_XversionX module:sxdocs_XversionX path:null spec-version:3.0]" failed, java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '0' bytes instead of stated: '-1' bytes..
java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '0' bytes instead of stated: '-1' bytes.
    at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:463)
>

请求已解雇:

POST /sxdocs_XversionX/faces/main.jsf?Adf-Window-Id=w0&Adf-Page-Id=1 HTTP/1.1

pageFlowScope.InkassPorReportBean.dlw:是支持bean中的动作侦听器方法,执行文件下载过程,如另一个问题的答案所示。

1 个答案:

答案 0 :(得分:0)

您需要使用fileDownloadActionListener这样的内容:

< af:commandMenuItem ...
    <af:filedownloadactionlistener contenttype="application/octet-stream"
                method="#{backingBean.doDownload}"  filename="defaultFilename.txt"/>           
</af:commandMenuItem >

 public void doDownload(FacesContext facesContext, OutputStream outputStream) {
      // write the neccessary code to get the download data from the Model
      String data = getDownloadData();

      // save to the output stream
      try {
          OutputStreamWriter writer = new OutputStreamWriter(outputStream,"UTF-8");
           writer.write(data);
           writer.close();
          outputStream.close();
      } catch (IOException e) {
          // handle I/O exceptions
      }
  }

更多参考资料:

http://adfcodebits.blogspot.co.uk/2010/06/bit-19-downloading-file.html https://tompeez.wordpress.com/2011/11/26/jdev11-1-2-1-0-handling-imagesfiles-in-adf-part-2/