3

I am downloading zip file from web server using Java in my jsf bean. My code works for JPEG well but not for ZIP. Here is my code.

private void createDownloadFile(final URL downloadUrl, final String mimeType) {
    final FacesContext fc = FacesContext.getCurrentInstance();
    final ExternalContext context = fc.getExternalContext();
    final HttpServletResponse response = (HttpServletResponse) context.getResponse();
    response.setContentType(mimeType);
    response.addHeader("Content-Disposition", "attachment; filename=\"" + downloadUrl + "\"");

    try{
        final OutputStream out = response.getOutputStream();
        IOUtils.copy(downloadUrl.openStream(), out);
        fc.responseComplete();
    }catch (final IOException exc){
      exc.printStackTrace();
    }

}

And this is the error: Empty response, an empty response was recieved from the server. Any help is appreciated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Laura
  • 33
  • 3

1 Answers1

1

Empty response, an empty response was recieved from the server.

This message is recognizeable as a XML parsing error in Chrome; the XML parser couldn't find the requested XML root element. In combination with whatever you're trying to do, this suggests that you were trying to download the file by JavaScript/Ajax. You cannot do this because JavaScript has due to obvious security reasons no facilities to force a Save As dialog with file content which is been hold in a variable.

This error message is caused because JSF ajax responses are definied to return a specific XML format, <partial-response><update>, etc. But if the <partial-response> root element is missing in the JSF ajax response, then the browser-specific XML parser will/may show this kind of error.

You need to download the file by a synchronous request instead. Remove the <f:ajax> or set whatever ajax-toggling attribute of the 3rd party component to false, such as <p:commandButton ajax="false">.

You don't need to worry about a page change in the browser, as you've set the Content-Disposition: attachment response header, the current page will just remain the same. This problem is in no way specific to Commons IO, by the way.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555