我得到java.lang.IllegalStateException:已经为此响应调用了getOutputStream()异常

时间:2013-07-08 09:39:07

标签: java servlets

java.lang.IllegalStateException:已为此响应调用了getOutputStream()

嗨我正试图通过我的java代码打开pdf文件时遇到异常

这是我的Java代码。我正在使用IText创建pdf

@RequestMapping(value = "/pdf", method = RequestMethod.POST)
public String pdf(ActivitiesForm activitiesForm, Model model,
        HttpSession session,HttpServletResponse response) throws InstanceNotFoundException, IOException,
        DocumentException {

    String PATH = "/home/space/workspace/trainning/Trainning/Performance.pdf";
    // step 1
    Document document = new Document();
    // step 2
     FileOutputStream fos = new FileOutputStream(PATH);
    PdfWriter.getInstance(document,fos);
    // step 3
    document.open();
    // step 4
    PdfPTable table;

    table = new PdfPTable(3);
    table.setWidthPercentage(100);
    PdfPCell cell;

    cell = new PdfPCell(new Phrase("Roll No"));
    cell.setColspan(1);
    // cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Name"));
    cell.setColspan(1);
    // cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Performance"));
    cell.setColspan(1);
    // cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    Integer count = rollService.getRollCount();
    Integer countpts = creditptsService.getPtsCount();

    if (countpts != null) {
        for (Integer i = 1; i <= count; i++) {
            Roll roll = rollService.getRollNoById(Long.parseLong(i
                    .toString()));

            List<Creditpts> creditpts = creditptsService.getRollById(roll
                    .getId());

            Integer performance = 0;

            if (creditpts == null) {

                cell = new PdfPCell(new Phrase("" + roll.getRollno()));
                cell.setColspan(1);
                // cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase("" + roll.getStudentName()));
                cell.setColspan(1);
                // cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase("0"));
                cell.setColspan(1);
                // cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

            } else {
                Iterator<Creditpts> creIterator = creditpts.iterator();

                while (creIterator.hasNext()) {
                    Creditpts creditpt = creIterator.next();

                    performance += creditpt.getCrdpts();
                }

                cell = new PdfPCell(new Phrase("" + roll.getRollno()));
                cell.setColspan(1);
                // cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase("" + roll.getStudentName()));
                cell.setColspan(1);
                // cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

                cell = new PdfPCell(new Phrase("" + performance));
                cell.setColspan(1);
                // cell.setBorder(Rectangle.NO_BORDER);
                table.addCell(cell);

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

    fos.close();
    FileInputStream baos = new FileInputStream(PATH);

    // set some response headers
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");
    response.setContentType("application/pdf");

    response.setContentLength((int) new File(PATH).length());
    OutputStream os = response.getOutputStream();
    byte buffer[] = new byte[8192];
    int bytesRead, i;

    while ((bytesRead = baos.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    os.flush();
    os.close();
}

我已粘贴我的pdf创建和打开代码 我在这里做错了我错过了一些事情......

这是完整的堆栈跟踪:

java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:633)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:336)
at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:353)
at org.apache.jsp.jsp.activity_jsp._jspService(activity_jsp.java:402)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:792)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

3 个答案:

答案 0 :(得分:3)

在try / catch的末尾添加以下内容,以避免JSP引擎通过getWriter()

刷新响应时出现的错误
out.clear(); // where out is a JspWriter
out = pageContext.pushBody();

这不是最佳做法,但可以避免日志中的错误。 理想情况下,我们应该在Servlet中而不是在JSP中执行此操作。如果您仍然需要在JSP中使用它,请按照上面的代码来消除错误。

答案 1 :(得分:2)

您完全在java代码中处理请求,然后Spring /容器的功能通过呈现一些jsp来尝试处理相同的请求。在这种特殊情况下,您需要禁用此功能。

答案 2 :(得分:0)

正如您在堆栈跟踪中看到的那样,JSP调用response.getWriter(),但异常消息显示之前调用了response.getOutputStream()。您不能在同一个响应对象上调用这两个方法。

检查以下链接:

https://stackoverflow.com/a/3540474/2333119

https://stackoverflow.com/a/14951581/2333119