通过servlet post下载JasperReports生成的pdf时出错

时间:2017-04-12 22:12:19

标签: java servlets jasper-reports pdf-generation

我生成了报告,用户只能收到一篇文章,并以相同报告的正常格式完成。

按照密码,如果你可以帮助谢谢你,我几天前就已经用它了,这肯定是我的错,但我看不到它。

按照代码:

public class DownloadNovadata extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Inject
    private UsuariosNovadata usuariosNovadata;

    private String dni;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println("Entrei no doGet");
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        dni = req.getParameter("dni");

        HashMap<String, Object> parametros = new HashMap<>();
        UsuarioNovadata ugc = usuariosNovadata.porDni(dni);
        parametros.put("p_Novadata_id", ugc.getNovadata().getId());
        String nomeRelatorio = "NovadataSQL";

        HashMap paramRel = new HashMap();
        String imagemLogo = "/Users/images/iofe.jpg";
        paramRel.put("imagemLogo", imagemLogo);
        paramRel.put("REPORT_LOCALE", new Locale("pt", "BR"));
        JasperPrint print = null;
        try {
            Connection con = null;
            String driver = "org.postgresql.Driver";
            String user = "userName";
            String senha = "password";
            String url = "jdbc:postgresql://site:5432/dbname";
            try {
                Class.forName(driver);
                con = (Connection) DriverManager.getConnection(url, user, senha);
            } catch (ClassNotFoundException ex) {
                System.err.print(ex.getMessage());
            } catch (SQLException e) {
                System.err.print(e.getMessage());
            }
            print = JasperFillManager
                    .fillReport("/relatorios/" + nomeRelatorio + ".jasper", paramRel, con);
            JRPdfExporter exporter = new JRPdfExporter();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            JasperExportManager.exportReportToPdfStream(print, baos);
            ServletOutputStream out = res.getOutputStream();
            res.reset();
            res.setContentType("application/pdf");
            res.setHeader("Content-disposition","inline; filename=" + dni + ".pdf");
            res.setHeader("Cache-Control", "no-cache");
            res.setDateHeader("Expires", 0);
              baos.writeTo(out);
              out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }
}

0 个答案:

没有答案