简体   繁体   English

我正在获取java.lang.IllegalStateException:此响应已调用getOutputStream()异常

[英]I m getting java.lang.IllegalStateException: getOutputStream() has already been called for this response Exception

java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:已为此响应调用getOutputStream()

Hi I'm getting above exception while m trying to open a pdf file through my java code 嗨,我在尝试通过我的Java代码打开pdf文件时遇到了异常

Here is my Java code. 这是我的Java代码。 I am using IText to create pdf 我正在使用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();
}

I have paste my pdf creation and opening code what i'm doing wrong here mi missing some thing... 我粘贴了我的pdf创建和打开代码,我在这里做错了,我错过了一些东西...

Here is full stack trace: 这是完整的堆栈跟踪:

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)

Add the following inside the end of the try/catch to avoid the error that appears when the JSP engine flushes the response via getWriter() 在try / catch的末尾添加以下内容,以避免JSP引擎通过getWriter()刷新响应时出现的错误。

out.clear(); // where out is a JspWriter
out = pageContext.pushBody();

This isn't best practice, but it avoids the errors in your logs. 这不是最佳做法,但可以避免日志中的错误。 Ideally we should do this in Servlet and not in JSP. 理想情况下,我们应该在Servlet中而不是在JSP中执行此操作。 If you still needs it in JSP, follow above code to get rid off the error. 如果在JSP中仍然需要它,请按照上面的代码摆脱错误。

You handle the request completely in java code, and then the Spring/container's functionality kicks in trying to handle the same request by rendering some jsp. 您可以完全用Java代码处理请求,然后Spring /容器的功能通过呈现一些jsp来尝试处理相同的请求。 You need to disable this functionality in this particular case. 在这种特殊情况下,您需要禁用此功能。

As you can see in the stack trace, the JSP calls response.getWriter(), but the exception message says there was a call to response.getOutputStream() before. 如您在堆栈跟踪中所见,JSP调用response.getWriter(),但异常消息表明之前已经调用过response.getOutputStream()。 You cannot call both methods on the same response object. 您不能在同一个响应对象上调用这两种方法。

check below links: 检查以下链接:

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

and

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我得到一个异常:java.lang.IllegalStateException:已经为此响应调用了getOutputStream() - I am getting an exception: java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:已经为此响应调用了 getOutputStream() - java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:已为此响应调用了getOutputStream() - java.lang.IllegalStateException: getOutputStream() has already been called for this response 获取Exception-java.lang.IllegalStateException:此响应已调用getOutputStream() - Getting Exception-java.lang.IllegalStateException: getOutputStream() has already been called for this response 原因:java.lang.IllegalStateException:此响应已经调用了getOutputStream()。 - Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response org.apache.jasper.JasperException:java.lang.IllegalStateException:此响应已调用getOutputStream() - org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response Spring Boot Web应用程序:引发java.lang.IllegalStateException:此响应已调用getOutputStream() - Spring Boot Web Application: throwing java.lang.IllegalStateException: getOutputStream() has already been called for this response REST-java.lang.IllegalStateException:此响应已调用getOutputStream() - REST - java.lang.IllegalStateException: getOutputStream() has already been called for this response Spring MVC: java.lang.IllegalStateException: getOutputStream() 已经被这个响应调用 - Spring MVC : java.lang.IllegalStateException: getOutputStream() has already been called for this response Spring:java.lang.IllegalStateException:已经为此响应调用了getOutputStream() - Spring: java.lang.IllegalStateException: getOutputStream() has already been called for this response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM