Boa tarde amigos,
estou uma duvida bem batida já…
ok vamos la!
seguinte, tenho um commandButton que chama um download la no mey Bean… funciona de boa ! mas quando eu clico em qualquer outro botao( que nao pe download)
o download anterior continua aparecendo pra eu baixar de novo!
ou seja… quando eu clico pra fazer o download, e vou para uma outra pagina… o download aparece, dai clico em cancelar… e continuo navegando… e deposi aparece dinovo!
parece que ele ficou no header da pagina… seila… como posso resolver isso ??
estou fazendo assim:
<h:commandButton value="Download" action="#{Filebean.downloadFile}" immediate="true"/>
public String downloadFile(){
FileInputStream fis;
byte[] fileBlob;
int fileLength = 0;
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletRequest req = (HttpServletRequest) facesContext.getExternalContext().getRequest();
DataTableArquivo dta = (DataTableArquivo) req.getAttribute("dta");
try {
File f = getRealFile(dta.getArquivo());
fis = new FileInputStream(f);
fileLength = (int) f.length() + 1;
fileBlob = new byte[fileLength];
fis.read(fileBlob);
fis.close();
writeBytes(fileBlob, f.getName());
} catch (IOException ex) {
Logger.getLogger(Filebean.class.getName()).log(Level.SEVERE, null, ex);
}
return "";
}
private void writeBytes(byte[] bytes,String fileName){
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
if (bytes != null && bytes.length > 0) {
ServletOutputStream ouputStream = null;
try {
response.setContentType("application/force-download");
response.setHeader("Content-disposition", "attachment;filename="+fileName);
response.setContentLength(bytes.length);
ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
facesContext.responseComplete();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
obrigado desde ja