本文主要是介绍Jasperreports+jaspersoft studio 实现单个或多个jrxml(jasper)文件生成一个pdf文件,并利用Servlet发送该pdf文件到浏览器中展示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Jasperreports+jaspersoft studio 实现单个或多个jrxml(jasper)文件生成一个pdf文件,并利用Servlet发送该pdf文件到浏览器中展示;
代码如下:
Demo07.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Demo07" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0431a06d-749b-411d-8fbf-592aa6b1cca1"><property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/><queryString><![CDATA[]]></queryString><background><band splitType="Stretch"/></background><detail><band height="125" splitType="Stretch"><staticText><reportElement x="148" y="34" width="100" height="30" uuid="2bea566e-a20b-4bd6-abe0-0f7760753a24"/><text><![CDATA[Hello World!]]></text></staticText></band></detail>
</jasperReport>
Demo08.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Demo08" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="eba4c4b3-0a9c-43a5-b941-5f9d359ffaf4"><property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/><queryString><![CDATA[]]></queryString><background><band splitType="Stretch"/></background><detail><band height="125" splitType="Stretch"><staticText><reportElement x="172" y="38" width="100" height="30" uuid="416b6928-db60-40fb-b416-46dbc989203c"/><text><![CDATA[hahahalouu]]></text></staticText></band></detail>
</jasperReport>
JasperServletMul.java
package com.accord.servlet;import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;public class JasperServletMul extends HttpServlet {private static final long serialVersionUID = 1L;public JasperServletMul() {super();}public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@SuppressWarnings("deprecation")public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ServletContext context = this.getServletConfig().getServletContext(); try {//JasperCompileManager.compileReportToFile("E:/report/DbReport.jrxml");String filename = JasperCompileManager.compileReportToFile(context.getRealPath("/jasper/Demo07.jrxml"));String filename02 = JasperCompileManager.compileReportToFile(context.getRealPath("/jasper/Demo08.jrxml"));System.out.println(filename);System.out.println(filename02);} catch (JRException e1) {e1.printStackTrace();}//编译jrxml文件,生成jasper文件// 第一步:装载jasper文件File jasperFileName = new File(context.getRealPath("/jasper/Demo07.jasper"));// 第二步:设置参数值/* 设置参数 */HashMap<String, Object> params2 = new HashMap<String, Object>();// 建立参数表params2.put("name","我们的产品"); // 设置参数值// 第三步:利用JasperRunManager生成PDF文件try {JasperRunManager.runReportToPdfFile(jasperFileName.getPath(),params2, new JREmptyDataSource());} catch (JRException e) {e.printStackTrace();}JRPdfExporter jrpdf = new JRPdfExporter();JasperReport report1 = null;try {report1 = (JasperReport) JRLoader.loadObject(new File(context.getRealPath("/jasper/Demo07.jasper")));} catch (JRException e2) {e2.printStackTrace();}JasperReport report2 = null;try {report2 = (JasperReport) JRLoader.loadObject(new File(context.getRealPath("/jasper/Demo08.jasper")));} catch (JRException e1) {e1.printStackTrace();}List<String> list = new ArrayList<String>();list.add("测试数据");JRBeanCollectionDataSource ds1 = new JRBeanCollectionDataSource(list);JRBeanCollectionDataSource ds2 = new JRBeanCollectionDataSource(list);HashMap<String, Object> params = new HashMap<String, Object>();// 建立参数表params.put("name", "我们的产品"); // 设置参数值JasperPrint jasperPrint = null;try {jasperPrint = JasperFillManager.fillReport(report1,params,ds1);} catch (JRException e1) {e1.printStackTrace();}JasperPrint jasperPrint2 = null;try {jasperPrint2 = JasperFillManager.fillReport(report2,params,ds2);} catch (JRException e1) {e1.printStackTrace();}List jasperPrintList = new ArrayList();jasperPrintList.add(jasperPrint);jasperPrintList.add(jasperPrint2);String filename = context.getRealPath("/jasper/")+"/" + (System.currentTimeMillis() + "" )+ ".pdf";File file = new File(filename);ByteArrayOutputStream baos = new ByteArrayOutputStream();JRPdfExporter exporter = new JRPdfExporter();exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);try {exporter.exportReport();} catch (JRException e1) {e1.printStackTrace();}byte[] bytes = baos.toByteArray();// 确定写出文件的位置try {// 建立输出字节流FileOutputStream fos = new FileOutputStream(file);// 用FileOutputStream 的write方法写入字节数组fos.write(bytes);System.out.println("写入成功");// 为了节省IO流的开销,需要关闭fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}//下面代码是实现在浏览器中显示pdf文件 开始://设置发送到客户端响应的内容类型,浏览器会根据不同的MIME,调用不同的模块处理response.setContentType("application/pdf");ServletOutputStream out = response.getOutputStream();File pdf = null;BufferedInputStream buf = null;try {pdf = new File(filename);response.setContentLength((int)pdf.length());FileInputStream input = new FileInputStream(pdf);//带缓冲区的输入流//FileInputStream是字节流,BufferedInputStream是字节缓冲流,使用BufferedInputStream读资源比FileInputStream读取资源的效率高buf = new BufferedInputStream(input);int readBytes = 0;while((readBytes=buf.read())!=-1) {out.write(readBytes);}} catch (Exception e) {System.out.println("文件没有找到");} finally {if(out!=null) {out.close();}if(buf!=null) {buf.close();}}//上面代码是实现在浏览器中显示pdf文件 结束: }
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>JasperReports</groupId><artifactId>JasperReports02</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>JasperReports02 Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>5.6.0</version> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.30</version></dependency><!-- 解决中文乱码 --><dependency> <groupId>com.lowagie</groupId><artifactId>itextasian</artifactId><version>1.5.2</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency></dependencies><build><finalName>JasperReports02</finalName></build>
</project>
web.xml
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><servlet><servlet-name>JasperServlet</servlet-name><display-name>JasperServlet</display-name><description></description><servlet-class>com.accord.servlet.JasperServlet</servlet-class></servlet><servlet><servlet-name>JasperServletDb</servlet-name><display-name>JasperServletDb</display-name><description></description><servlet-class>com.accord.servlet.JasperServletDb</servlet-class></servlet><servlet><servlet-name>JasperServletJavaBean</servlet-name><display-name>JasperServletJavaBean</display-name><description></description><servlet-class>com.accord.servlet.JasperServletJavaBean</servlet-class></servlet><servlet><servlet-name>JasperServletJavaBeanGroup</servlet-name><display-name>JasperServletJavaBeanGroup</display-name><description></description><servlet-class>com.accord.servlet.JasperServletJavaBeanGroup</servlet-class></servlet><servlet><servlet-name>JasperServletTable</servlet-name><display-name>JasperServletTable</display-name><description></description><servlet-class>com.accord.servlet.JasperServletTable</servlet-class></servlet><servlet><servlet-name>JasperServletChart</servlet-name><display-name>JasperServletChart</display-name><description></description><servlet-class>com.accord.servlet.JasperServletChart</servlet-class></servlet><servlet><servlet-name>JasperServletChart02</servlet-name><display-name>JasperServletChart02</display-name><description></description><servlet-class>com.accord.servlet.JasperServletChart02</servlet-class></servlet><servlet><servlet-name>JasperServletCh</servlet-name><display-name>JasperServletCh</display-name><description></description><servlet-class>com.accord.servlet.JasperServletCh</servlet-class></servlet><servlet><servlet-name>JasperServletMul</servlet-name><display-name>JasperServletMul</display-name><description></description><servlet-class>com.accord.servlet.JasperServletMul</servlet-class></servlet><servlet-mapping><servlet-name>JasperServlet</servlet-name><url-pattern>/JasperServlet</url-pattern></servlet-mapping><servlet-mapping><servlet-name>JasperServletDb</servlet-name><url-pattern>/JasperServletDb</url-pattern></servlet-mapping><servlet-mapping><servlet-name>JasperServletJavaBean</servlet-name><url-pattern>/JasperServletJavaBean</url-pattern></servlet-mapping><servlet-mapping><servlet-name>JasperServletJavaBeanGroup</servlet-name><url-pattern>/JasperServletJavaBeanGroup</url-pattern></servlet-mapping><servlet-mapping><servlet-name>JasperServletTable</servlet-name><url-pattern>/JasperServletTable</url-pattern></servlet-mapping><servlet-mapping><servlet-name>JasperServletChart</servlet-name><url-pattern>/JasperServletChart</url-pattern></servlet-mapping><servlet-mapping><servlet-name>JasperServletChart02</servlet-name><url-pattern>/JasperServletChart02</url-pattern></servlet-mapping><servlet-mapping><servlet-name>JasperServletCh</servlet-name><url-pattern>/JasperServletCh</url-pattern></servlet-mapping><servlet-mapping><servlet-name>JasperServletMul</servlet-name><url-pattern>/JasperServletMul</url-pattern></servlet-mapping>
</web-app>
浏览器访问如下:
以上就完成了:
这篇关于Jasperreports+jaspersoft studio 实现单个或多个jrxml(jasper)文件生成一个pdf文件,并利用Servlet发送该pdf文件到浏览器中展示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!