Pass a name to a downloaded file with <p:fileDownload>

Asked

Viewed 74 times

1

I have a method that generates a PDF report with Jasper and returns a DefaultStreamedContent. I call the method no <p:fileDownload> thus:

 <p:fileDownload value="#{listarReembolsoBean.getSampleReportPDF(reembolso)}" />

That is the method:

public StreamedContent getSampleReportPDF(Solicitacao rel) {
        InputStream relatorio = null;
        Map<String, Object> filtro = new HashMap<String, Object>();
        System.out.println("Codigo solicitacao:" + rel.getCodigo());

        try {
            InputStream image = this.getClass().getClassLoader().getResourceAsStream("Report/logo-unimed-correto.png");
            filtro.put("codigo", rel.getCodigo());
            filtro.put("image", image);
            String pdfFile = "C:\\sampleReport.pdf";

            ByteArrayOutputStream Teste = new ByteArrayOutputStream();

            JasperReport jasperReport = (JasperReport) JRLoader
                    .loadObject(getClass().getClassLoader().getResourceAsStream("Report/RelatorioReembolso.jasper"));
            jasperReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);

            Map<String, Object> params = new HashMap<String, Object>();
            JasperPrint print = JasperFillManager.fillReport(jasperReport, filtro, ConexaoMysql.abrir());
            System.out.println("Conexao aberta: " + print);

            JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();

//          exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, Teste);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.exportReport();

            relatorio = new ByteArrayInputStream(Teste.toByteArray());

        } catch (JRException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return new DefaultStreamedContent(relatorio);

    }

But when you open the file download box the name is as NULL, where can I set a name for that file?

inserir a descrição da imagem aqui

1 answer

1


Problem solved, just changed the return of the method in this way:

return new DefaultStreamedContent(relatorio, "application/pdf", "RelatorioReembolsoGeral.pdf");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.