1

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

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)
4

1 回答 1

1

在页面顶部使用 this

<%@ page trimDirectiveWhitespaces="true" %>

我们还应该为您添加flush/close数据输出。

dataOutput.flush();
dataOutput.close();
return;
于 2012-09-03T10:10:38.910 回答