0

报告.java

     byte[] pdfbyte = null;
        try {
            dbcon connect = new dbcon();
            ServletContext context = this.getServletConfig().getServletContext();
            String compiledFileName = context.getRealPath("/jasper/report3.jasper");
            System.out.print(compiledFileName);
            // String var = request.getParameter("var").toString();
            //String res = JOptionPane.showInputDialog(null, "What is your name?", "Enter your name",JOptionPane.QUESTION_MESSAGE);

            File reportFile = new File(compiledFileName);
            HashMap parameters = new HashMap();
//            // parameters.put("itemName", var);
            parameters.put("ReportTitle", "MyReport");
//            parameters.put("BaseDir", reportFile.getParentFile());
//            parameters.put("EmpNo", 32504);
            JasperPrint jasperPrint = null;
            Connection conn = connect.dbstate().getConnection();
            try {

                try {
                    jasperPrint = JasperFillManager.fillReport(compiledFileName, parameters, conn);
                    pdfbyte = JasperExportManager.exportReportToPdf(jasperPrint);
                } catch (Exception e) {
                    System.out.print("E" + e);
                }
            } catch (Exception ex) {
                System.out.println("*** Could not create compiled jasper file: " + ex.getMessage());
            } finally {
                try {
                    connect.dbstate().close();
                } catch (SQLException ex) {
                    System.out.println("could not close  databse after filling the report ");
                }
            }
            ServletOutputStream outstream = response.getOutputStream();

            response.setContentType("application/pdf");
            response.setContentLength(pdfbyte.length);
            response.setHeader("Content-disposition", "inline; filename=\"Report.pdf\"");
            outstream.write(pdfbyte);
        } catch (Exception e) {
            System.out.print(e);
            e.printStackTrace();
        }

图书馆

在此处输入图像描述

我在 Netbeans 上遇到以下错误

SEVERE: A web application registered the JBDC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Ejava.lang.NullPointerExceptionjava.lang.NullPointerExceptionjava.lang.NullPointerException
        at jasper.Report.processRequest(Report.java:63)
        at jasper.Report.doGet(Report.java:75)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
        at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
        at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
        at java.lang.Thread.run(Thread.java:619)
4

2 回答 2

1

我建议您像@MadProgrammer 所说的那样预编译您的报告。要将报告从 servlet 发送到客户端(webbrowser 上的 previwe 或直接下载),我使用以下命令:

...
// trying get the report
        try {

        // getting de real path
        String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(reportFileName);

        //
        HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

        // here you can specify the condition to print, if conditionVoolean is true it will do a direct download
        if (conditionBoolean) {
            fileName = "your file name to direct download";
            httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + fileName +".pdf");
        }

        // 
        byte[] bytes;

        // get the filled report in bytes
        bytes = JasperRunManager.runReportToPdf(jasperReport, parameter, conn);

        if (bytes != null && bytes.length > 0) {

            // specifying the type of file/content to browser
            httpServletResponse.setContentType("application/pdf");

            httpServletResponse.setContentLength(bytes.length);

            ServletOutputStream ouputStream = httpServletResponse.getOutputStream();

            ouputStream.write(bytes, 0, bytes.length);

            ouputStream.flush();

            // closing the stream line
            ouputStream.close();
        }
        FacesContext.getCurrentInstance().responseComplete();
    }
    catch (JRException ex) {
        ex.printStackTrace();
    }
    catch (IOException ex) {
        ex.printStackTrace();
    }
...

Att:抱歉葡萄牙语的评论。它是我的母语。

希望对你有帮助^^

于 2013-09-26T10:05:03.393 回答
0

代码没有任何问题,我将所有库更改为最新更新的库,jasperreport-4.5.1.jar并且jasperreport-5.1.0.jar它有效!

于 2013-10-01T17:21:32.900 回答