【问题标题】:How to provide a file download from a JSF backing bean using af:commandMenuItem?如何使用 af:commandMenuItem 从 JSF 支持 bean 提供文件下载?
【发布时间】:2015-08-24 10:50:38
【问题描述】:

这是以下问题的精确副本: 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:是backing bean中的action listener方法,执行文件下载过程,如另一个问题的答案所示。

【问题讨论】:

  • 请从开发者的角度阐述问题,而不是从最终用户的角度。触发什么样的 HTTP 请求?动作监听器方法是否被触发? HTTP 响应包含什么?等等。
  • @BalusC 我如何收集这些信息?除了使用来自不同名称空间的不同对象之外,我已经按照您在答案中描述的方式完成了所有操作。
  • 只需在浏览器的 HTTP 流量监视器中查看请求/响应详细信息(按 F12 并检查“网络”选项卡)并在操作方法中添加断点(或记录器或穷人的系统输出)。基本调试,你应该已经知道了……
  • @BalusC 我设法找到以下内容:POST /sxdocs_XversionX/faces/main.jsf?Adf-Window-Id=w0&Adf-Page-Id=1 HTTP/1.1

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


【解决方案1】:

你需要像这样使用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/

【讨论】:

  • 问题是 是我动态生成文件,在某些情况下文件创建可能会失败,但是当我不想要任何东西时方法仍然继续并发送一个损坏的文件(不完整的输出流)发生
  • 这是正确刷新 IO 流的问题,与 ADF 无关。
  • 不,有时没有可用的数据,所以生成文件的代码会抛出异常。我需要然后取消下载,但我不能。我可以将输出流设置为空。但文件仍然被下载(空)@Florin
  • 然后单独开始文件创建,只有成功后才开始下载
  • 好的,这是有道理的。我会在菜单项上使用“禁用”条件,以便仅在文件数据可用时才允许单击按钮。
猜你喜欢
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多