0

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.

Community
  • 1
  • 1
Bato-Bair Tsyrenov
  • 1,174
  • 3
  • 17
  • 40
  • Please elaborate the problem in developer's perspective, not in enduser's perspective. What kind of HTTP request is fired? Is the action listener method fired? What does the HTTP response contain? And so forth. – BalusC Jun 09 '15 at 14:52
  • @BalusC How can I gather this inforamtion? I have done everything the way you described in the answer apart from using different object from different namespace. or – Bato-Bair Tsyrenov Jun 09 '15 at 15:24
  • Just look in browser's HTTP traffic monitor for request/response detail (press F12 and check Network tab) and add a breakpoint (or a logger or a poor man's sysout) to action method. Basic debugging, you should already know ... – BalusC Jun 09 '15 at 15:25
  • @BalusC I managed to find out following: POST /sxdocs_XversionX/faces/main.jsf?Adf-Window-Id=w0&Adf-Page-Id=1 HTTP/1.1 – Bato-Bair Tsyrenov Jun 09 '15 at 15:52

1 Answers1

0

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/

Florin Marcus
  • 1,657
  • 1
  • 12
  • 11
  • Problem is that with is that I generate file dynamically and in some cases file creation may fail, but method still continues and sends a broken file (incomlete outputstream) when i dont want anything to happen – Bato-Bair Tsyrenov Jun 09 '15 at 16:03
  • That is a matter of properly flushing the IO streams, not related to ADF. – Florin Marcus Jun 09 '15 at 16:05
  • Nope, sometimes there is no data available, so code that generates file throws an exception. I need to then cancel download but i can't. I can set outputstream to null. But file still gets downloaded (empty) @Florin – Bato-Bair Tsyrenov Jun 09 '15 at 16:09
  • 1
    Then start file creation separately and only if that succeeds, start the download – Kukeltje Jun 09 '15 at 16:41
  • Ok, this makes sense. I would use "disabled" condition on the menu item to allow button clicking only when file data is available. – Florin Marcus Jun 09 '15 at 18:37