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: