How to provide a file download from a JSF backing bean using af:commandMenuItem?

2.5k views Asked by At

This is an exact copy of the following question: How to provide a file download from a JSF backing bean?

Apart from I am trying to use different faces and the method described doesn't work.

Note: I cannot use

When using - file downloads fine.

<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">

I am trying to use and the page just refreshes, I dont know if it has ajax enabled for request but described methods in the answer do not work.

Here is my code for an object:

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

I also get following error if I set content length to file size, this doesnt happen if I response size is not set:

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

Request fired:

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

pageFlowScope.InkassPorReportBean.dlw: is the action listener method in backing bean that executes file download procedure as shown in the answer to another question.

1

There are 1 answers

5
Florin Marcus On BEST ANSWER

You need to use a fileDownloadActionListener something like this:

< 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
      }
  }

More references:

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/