我在JSP servlet中使用JasperReports生成 pdf。我将字符串参数传递给报告。当我尝试生成单个 pdf 时,生成的 pdf 很好,但是当我尝试生成两个 pdf 时,对于第一个参数,生成 pdf,但对于第二个参数,不生成 pdf。
我通过循环传递参数。
String array[]={"1233","234"};
HashMap params = new HashMap();
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection cn=DriverManager.getConnection("jdbc:oracle:thin:@103.179.1.218:1521:tims","scoot","tiger");
for(int i=0;i<array.length;i++) {
params.put("seller_name",array[i]);
JasperDesign jasperDesign = JRXmlLoader.load(getServletContext().getRealPath("/")+"report\\CForm.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperprint = JasperFillManager.fillReport(jasperReport,params, cn);
byte[] pdfasbytes = JasperExportManager.exportReportToPdf(jasperprint);
ServletOutputStream outstream=response.getOutputStream();
response.setContentType("application/pdf");
response.setContentLength(pdfasbytes.length);
String fileName="report"+i+".pdf";
response.setHeader("Content-disposition", "attachement; filename="+fileName);
outstream.write(pdfasbytes);
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperprint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outstream);
params.clear();
}