E ai pessoal,
Estou com um sério problema com o download de arquivos com o JSF. Sei que existem vários tópicos que mencionam o mesmo assunto porém não vi ngm com esse erro estranho que acontece cmg.
Resumo:
Tenho um datatable e com os dados do mesmo preciso fazer um download de um arquivo ".xls". Possuo um método que gera o arquivo normalmente e coloca numa pasta dentro do meu contexto web. Até ai tudo perfeito. Só que quando chamo o método pra fazer o download, ao invés de aparecer um dialog box pra salvar o arquivo está aparecendo umá página cheia de caracteres estranhos.
Será que algum de vcs tem alguma ideia pra solucionar esse mistério ?
Segue o meu método responsável pela lógica de download:/**
* Download a XLS file representing the deliveries filtered.
*
* @throws Exception
*/
public void downloadXLS() throws Exception {
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext ec = context.getExternalContext();
HttpServletResponse response = (HttpServletResponse) ec.getResponse();
File file = new File(regDoc.getAbsolutePath());
if (!file.exists()) {
log.warn("[ListDocumentGenerator - " + getCurrentLoggedUser().getUserid() + "] File '" + regDoc.getName() + "' doesn't exists.");
return;
}
if (!file.canRead()) {
log.warn("[ListDocumentGenerator - " + getCurrentLoggedUser().getUserid() + "] File '" + regDoc.getName() + "' can't be read.");
return;
}
// Verify if the size is within the limit of the response
long size = file.length();
int sizeInt = (int) size;
if (size != (long) sizeInt) {
log.warn("[ListDocumentGenerator - " + getCurrentLoggedUser().getUserid() + "] Size of the file '" + regDoc.getName() + "' (" + size + " bytes) doesn't fit the response's maximum size.");
return;
}
// Define the response length and type
response.setContentLength(sizeInt);
// see: http://www.w3schools.com/media/media_mimeref.asp
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
// Write the file's content to the response
byte[] b = new byte[sizeInt];
while (in.available() > 0) {
int qtty = in.read(b);
out.write(b, 0, qtty);
}
log.info("[ListDocumentGenerator - " + getCurrentLoggedUser().getUserid() + "] File '" + regDoc.getName() + "' (" + sizeInt + " bytes) was downloaded sucessfully.");
// Flush and close
in.close();
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
}
Any ideas ???? :roll:
