0

Hi I have been trying to write code so that i can generate a excel sheet and also download it when the user clicks on a download button....i have been successful in generating excel sheet but i have tried downloading the same but i have been unsuccessful.

the method i have used is:

public void download() throws IOException {
    File file = new File("D:\\pdf\\carrierReport7.xls");

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

    response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
    response.setHeader("Content-Type", "application/vnd.ms-excel");

    OutputStream outputStream = response.getOutputStream();

    FileInputStream fileInputStream = new FileInputStream(file);

    byte[] bytesBuffer = new byte[2048];

    int bytesRead = 0;

    while ((bytesRead = fileInputStream.read(bytesBuffer)) > 0) {
        outputStream.write(bytesBuffer, 0, bytesRead);
    }

    outputStream.flush();

    fileInputStream.close();
    outputStream.close();

    facesContext.responseComplete();
}

jsf command:

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
  • 1
    Are you seeing an error / exception? Does anything actually download to your browser when you try this? – Nicholas Hirras Jan 23 '17 at 14:13
  • Try to find one of the duplicates in stackoverflow and check what part of them helps you. And Nicolas Smith is right... you can and should provide more information. – Kukeltje Jan 23 '17 at 15:19
  • no error. its just processing the request and no reponse. also nothing is downloading. i tried different code already given in stackoverflow but still i am facing the same problem. Do i need to make any change in web.xml or browser setting – sanjay yadav Jan 24 '17 at 10:43

2 Answers2

1

i don't know if you check this, but ajax="false" must be declared in the commandButton...

<p:commandButton ajax="false" 
                 ..........
</p:commandButton>
nemesys
  • 11
  • 2
0

This is how I'm doing something similar in JSF 2, not sure what version you're on.

public void downloadAttachment(Attachment attachment) throws IOException {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();

    ec.responseReset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    ec.setResponseContentType(ec.getMimeType(attachment.getFilename()));        
    ec.setResponseContentLength(attachment.getFilesizeBytes().intValue()); // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
    ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + attachment.getFilename() + "\""); // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.

    OutputStream output = ec.getResponseOutputStream();
    output.write(attachment.getFileData());

    fc.responseComplete(); // Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed. }

}
Nicholas Hirras
  • 2,592
  • 2
  • 21
  • 28