Bom dia.
Tenho um commandLink
<h:commandLink value="#{baixa.arquivoRetorno.seqArquivo}" action="#{LinhaRetornoBean.Download}" >
<f:setPropertyActionListener value="#{baixa.arquivoRetorno.id}" target="#{LinhaRetornoBean.id}" />
</h:commandLink>
public String Download(){
arquivoRetorno = arquivoRetornoController.find(id);
OperacoesArquivosBean.downloadFile("teste.xls", "/upload/", "xls", FacesContext.getCurrentInstance());
return "download";
}
O método Download(), por sua vez chama o método downloadFile
Já tentei passar o caminho do arquivo de várias formas
/upload/teste.xls
upload/
upload
Ele está no contexto + a pasta /upload/
Qual a forma certa de passar o caminho relativo do arquivo?
public static synchronized void downloadFile(String filename, String fileLocation, String mimeType, FacesContext facesContext) {
ExternalContext context = facesContext.getExternalContext(); // Context
String path = fileLocation; // Localizacao do arquivo
String fullFileName = path + filename;
String contextPass = context.getRequestContextPath();
File file = new File(fullFileName); // Objeto arquivo mesmo :)
HttpServletResponse response = (HttpServletResponse) context.getResponse();
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
response.setContentLength((int) file.length()); // O tamanho do arquivo
response.setContentType(mimeType); // e obviamente o tipo
try {
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[(int)file.length()];
int count;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.flush();
out.close();
facesContext.responseComplete();
} catch (IOException ex) {
System.out.println("Error in downloadFile: " + ex.getMessage());
ex.printStackTrace();
}
}