iText pdf创建中的例外情况

时间:2012-09-03 09:20:49

标签: exception itext java-5

我正在尝试使用iText生成一个表(仅用于测试,这就是为什么我没有使用任何servlet和类)。我使用this教程作为我的参考。 代码没有显示任何错误,但在运行时出现异常

org.apache.jasper.JasperException: getOutputStream() has already been called for this response

我使用的是itext版本5.3.2,java 5.我的代码如下所示。

我的代码

<%@page import="java.io.DataOutputStream"%>
<%@page import="com.itextpdf.text.DocumentException"%>
<%@page import="java.io.DataOutput"%>
<%@page import="com.itextpdf.text.pdf.PdfPTable"%>
<%@page import="com.itextpdf.text.pdf.PdfWriter"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="com.itextpdf.text.Document"%>       
<%

response.setContentType("application/pdf");
Document document = new Document();
try{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
document.open();


PdfPTable table = new PdfPTable(2);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");


document.add(table);
document.close();

DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for(int i = 0; i < bytes.length; i++)
{
dataOutput.writeByte(bytes[i]);
}

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

%>

异常

org.apache.jasper.JasperException: getOutputStream() has already been called for this response
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.IllegalStateException: getOutputStream() has already been called for this response
    org.apache.catalina.connector.Response.getWriter(Response.java:606)
    org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
    org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
    org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:117)
    org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:191)
    org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115)
    org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
    org.apache.jsp.generatepdf_jsp._jspService(org.apache.jsp.generatepdf_jsp:99)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

1 个答案:

答案 0 :(得分:1)

页面顶部的

使用此

<%@ page trimDirectiveWhitespaces="true" %>

我们还应该为您的dataOutput添加flush / close

dataOutput.flush();
dataOutput.close();
return;