使用servlet java代码从jasper报告生成pdf文件

时间:2016-05-09 12:08:24

标签: servlets report

我有一个jasper报告,我把它放在与servlet相同的包中,我需要从中生成pdf文件,但是代码不起作用并且没有给出任何错误

public void print(String numBac , HttpServletRequest request,
  HttpServletResponse response) {
    try {          
        JasperDesign jasperDesign = JRXmlLoader
                .load("fiche.jrxml");
        String sql = "SELECT * FROM etudiant "
                + "INNER JOIN filiere ON etudiant.code_f=filiere.code_f "
                + "INNER JOIN lieu_nais ON etudiant.code_lieu=lieu_nais.code_lieu "
                + "INNER JOIN montant ON etudiant.code_m=montant.code_m WHERE bac='"
                + numBac + "'";
        JRDesignQuery newQuery = new JRDesignQuery();
        newQuery.setText(sql);
        jasperDesign.setQuery(newQuery);
        JasperReport report = JasperCompileManager
                .compileReport(jasperDesign);
        //JasperPrint print = JasperFillManager.fillReport(report, null, cnx);
        //JasperViewer.viewReport(print);

        byte[] byteStream;
        byteStream = JasperRunManager.runReportToPdf(report, null, cnx);
        OutputStream outStream = response.getOutputStream();
        response.setHeader("Content-Sisposition", "inline,filename="+DOWNLOAD_FILE_NAME);
        response.setContentType(FILE_TYPE);
        response.setContentLength(byteStream.length);
        outStream.write(byteStream, 0, byteStream.length);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

即使我想生成jasper预览它也没有显示(注释中的代码)

2 个答案:

答案 0 :(得分:1)

什么是宝贝?你需要冲洗/关闭sos吗?  您可以使用以下代码。这是使用java servlet的完整示例

  1. 请下载以下jar文件..
    1. 的JasperReports-5.0.1.jar
    2. 共享记录-1.1.2.jar
    3. 公地消化器-2.1.jar
    4. 公地集合-3.2.1-1.0.0.jar
    5. 公地BeanUtils的-1.8.3.jar
    6. Groovy的全2.1.3.jar
    7. com.lowagie.text-2.1.7.jar
    8. 您的数据库库
  2. 您可以使用

     @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         try {
            String path = "D:\\Software\\iReport-5.0.0-windows-installer\\u\\report4.jrxml";
            JasperReport jasReport = JasperCompileManager.compileReport(path);
            System.out.println("Jasper Report : " + jasReport);
            //Database connection 
            Connection con = /*Database Connection*/;
            System.out.println(con);
    
              //If You have any Paramert use bellow like
              Map paramMap = new HashMap();
             paramMap.put("id", request.getParameter("id"));
    
             //If You don't have parameter pass null instead of paramMap
            JasperPrint jasPrint = JasperFillManager.fillReport(jasReport, paramMap, con);
            System.out.println("Jasper Print : " + jasPrint);
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
            ServletOutputStream sos = response.getOutputStream();
            response.setContentType("application/pdf");
            JasperExportManager.exportReportToPdfStream(jasPrint, sos);
    
        } catch (JRException ex) {
            Logger.getLogger(Tests.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

答案 1 :(得分:0)

首先,这里有拼写错误:

response.setHeader("Content-Sisposition", "inline,filename="+DOWNLOAD_FILE_NAME);

它应该说“处置”而不是“Sisposition”:

response.setHeader("Content-Disposition", "inline,filename="+DOWNLOAD_FILE_NAME);

另外,请尝试使用“附件”而不是“内联”:

response.setHeader("Content-Disposition", "attachment,filename="+DOWNLOAD_FILE_NAME);

然后冲洗并关闭outStream:

outStream.flush();
outStream.close();

希望它有所帮助!