Hello,
I have a problem with a dowloaded excel file produced by POI. It says the
file is damaged...
Here is the method I use in order to download the file:
public String exportEntireTable() {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (!facesContext.getResponseComplete()) {
String fileName = "export.xls";//todo
ServletContext servletContext = (ServletContext)
facesContext.getExternalContext().getContext();
String contentType = servletContext.getMimeType (fileName);
HttpServletResponse response = (HttpServletResponse)
facesContext.getExternalContext().getResponse();
response.setContentType(contentType);
response.setHeader("Content-Disposition",
"attachment;filename=\"" + fileName + "\"");
try {
InputStream in = new
ByteArrayInputStream(retrieveEntireTable());//returns an array of bytes for
the file
//BlockingInputStream bis = new BlockingInputStream(in);
ServletOutputStream out = response.getOutputStream();
byte[] buf = new byte[512];
int bytesRead;
while ((bytesRead = in.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
out.flush();
facesContext.responseComplete();
} catch (IOException e) {
log.fatal("IOException", e);
throw new RuntimeException(e);
}
}
return null;
}
Can anyone please help?
Thanks in advance,
Julien.