我想查看我在运行时创建的jasper报告" PDFLEAD.jsp"页。但它显示为" getWriter()已被调用此响应。"它显示为下面给出的图像。我尝试的代码如下
PDFLEAD.jsp
<%@page import = "net.sf.jasperreports.engine.design.JRDesignQuery"%>
<%@page import = "net.sf.jasperreports.engine.xml.JRXmlLoader"%>
<%@page import = "net.sf.jasperreports.engine.design.JasperDesign"%>
<%@page import = "net.sf.jasperreports.view.JasperViewer"%>
<%@page import = "net.sf.jasperreports.engine.JRException"%>
<%@page import = "net.sf.jasperreports.engine.JRResultSetDataSource"%>
<%@page import = "net.sf.jasperreports.engine.JasperCompileManager"%>
<%@page import = "net.sf.jasperreports.engine.JasperExportManager"%>
<%@page import = "net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import = "net.sf.jasperreports.engine.JasperPrint"%>
<%@page import = "net.sf.jasperreports.engine.JasperReport"%>
<%@page import = "net.sf.jasperreports.engine.data.JRCsvDataSource"%>
<%@page import = "net.sf.jasperreports.engine.export.ooxml.JRDocxExporter"%>
<%@page import = "net.sf.jasperreports.engine.export.JRPdfExporter"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ include file="/Purchase.jsp" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.ParseException" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.FileNotFoundException" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.ByteArrayOutputStream" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%
Connection conn = null;
String sear=(String)session.getAttribute("sea");
String cate=(String)session.getAttribute("cat");
String comp=(String)session.getAttribute("com");
System.out.println("1 is:"+sear);
System.out.println("2 is:"+cate);
System.out.println("3 is:"+comp);
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
String jrxmlFile ="D:/dev/tools/jasper files/report10.jrxml";
InputStream input = new FileInputStream(new File(jrxmlFile));
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("complan",comp);
map.put("search",sear);
map.put("category",cate);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,map,conn);
byte bytes[] = new byte[10000];
JRPdfExporter exporter = new JRPdfExporter();
ByteArrayOutputStream Stream = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, Stream);
exporter.exportReport();
System.out.println("Size of byte array:"+Stream.size());
bytes = Stream.toByteArray();
response.setContentType("application/pdf");
System.out.println("After JasperPrint = 1");
response.setContentLength(bytes.length);
System.out.println("After JasperPrint = 2");
Stream.close();
System.out.println("After JasperPrint = 3");
OutputStream outputStream = response.getOutputStream();
System.out.println("After JasperPrint = 4");
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
outputStream.close();
}
catch(Exception e)
{e.printStackTrace();}
%>
</body>
</html>
答案 0 :(得分:1)
错误是您已经使用编写器发送<!DOCTYPE
和其他html标记(包括System.out)
将所有html标签移除为<html>,<head>,<title>
ecc。
删除System.out.println
(如果您的流式传输pdf内容,则无法使用此内容)
替换
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
使用
<%@ page contentType="application/pdf" %>
<%@ page trimDirectiveWhitespaces="true"%>
并传输你的pdf(我会这样做):
JasperExportManager.exportReportToPdfStream(jasperPrint,
response.getOutputStream());
因此只有这3个命令可以发送到浏览器......