I have a datatable and I want to implement a download button to download the pdf file from that particular line. I have already been able to implement p: fileUpload and now I want to download this file that was uploaded.
My upload method
public void upload(FileUploadEvent evento) {
try {
UploadedFile arquivoUpload = evento.getFile();
Path arquivoTemp = Files.createTempFile(null, null);
Files.copy(arquivoUpload.getInputstream(), arquivoTemp, StandardCopyOption.REPLACE_EXISTING);
monografia.setCaminho(arquivoTemp.toString());
Messages.addGlobalInfo("Upload realizado com sucesso");
} catch (IOException erro) {
Messages.addGlobalInfo("Ocorreu um erro ao tentar realizar o upload de arquivo");
erro.printStackTrace();
}
}
Method salvar
public void salvar(){
try {
Path origem = Paths.get(monografia.getCaminho());
Path destino = Paths.get("C:/Uploads/Monografia/" + monografia.getTitulo() + ".pdf");
Files.copy(origem, destino, StandardCopyOption.REPLACE_EXISTING);
cadastroMonografiaService.salvar(monografia);
atualizarRegistros();
messages.info("Monografia salva com sucesso!");
RequestContext.getCurrentInstance().update(Arrays.asList("frm:monografiasDataTable", "frm:messages"));
} catch (Exception erro) {
messages.alerta("Erro ao salvar a Monografia!");
erro.printStackTrace();
}
}