简体   繁体   English

第一次调用时response.getOutputStream()抛出IllegalStateException

[英]response.getOutputStream() throws IllegalStateException when called for the first time

I'm trying to Download a pdf from a MySQL-DB ( I get it as a Blob, and up to that point everything works just fine. But when I try to get the ServletOutputstream to send it to the client the programm crashes. 我正在尝试从MySQL-DB下载pdf(我以Blob的形式获取它,到那时为止一切正常。但是,当我尝试获取ServletOutputstream将其发送给客户端时,程序崩溃。

AFAIK the exception is thrown when the method has been called before or the getwriter-method has. AFAIK之前调用方法或getwriter方法已调用时,将引发异常。 But I don't use the getwriter-method in my code at all and the other getOutPutStream-methods in the programm are not reached (I commented them out to make sure). 但是我根本没有在代码中使用getwriter方法,并且程序中的其他getOutPutStream方法都没有达到(我将其注释掉以确保)。
(The whole thing happens in a JSP if that matters for any reason) (如果由于某种原因这很重要,那么整个事情就会在JSP中发生)

Heres my code snippet and the Exception: 这是我的代码段和异常:

Code

Blob pdf = null;
if(request.getParameter("reportId") != null){
    pdf = testszenario.getReportErgebnisPdf(Integer.parseInt(request.getParameter("reportId")), request.getParameter("erzeugung"));
}
byte[] buf = new byte[(int)pdf.length()];
InputStream inputStream  = pdf.getBinaryStream();
inputStream.read(buf);
inputStream.close();
response.setContentType("application/pdf");
response.setHeader("Content-disposition","attachment; filename=test.pdf"); 

ServletOutputStream sos = response.getOutputStream();
// response.getOutputStream().write(buf);

Exception 例外

26.03.2013 09:28:29 org.apache.catalina.core.ApplicationDispatcher invoke
SCHWERWIEGEND: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException
    at org.apache.jasper.runtime.ServletResponseWrapperInclude.getOutputStream(ServletResponseWrapperInclude.java:63)
    at org.apache.jsp.jsp.modules.Testszenario.PostReportResultOverview_jsp._jspService(PostReportResultOverview_jsp.java:115)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:535)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:472)
    at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968)
    at org.apache.jsp.jsp.McFrame_jsp._jspService(McFrame_jsp.java:284)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

Please check if your response is committed. 请检查您的回复是否已落实。 If you try to do something on a committed response then IllegalStateException may arise. 如果您尝试对已提交的响应进行操作,则可能会出现IllegalStateException Response will be committed, if page buffer size is exceeded. 如果超出页面缓冲区大小,将提交响应。

It might indeed matter that you're doing this in a JSP. 在JSP中执行此操作确实很重要。 Keep in mind that the JSP is part of the output rendering phase of a web application request, so the container might have obtained the writer object already. 请记住,JSP是Web应用程序请求的输出呈现阶段的一部分,因此容器可能已经获取了writer对象。 Try moving your code into a servlet instead. 尝试将代码移入servlet。 It'll give you more control over the writer/streams. 它可以让您更好地控制编写器/流。

暂无
暂无

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

相关问题 response.getOutputStream已被调用 - response.getOutputStream has already been called 在JSP中将MySQL请求抛出IllegalStateException时使用的response.getOutputStream - response.getOutputStream used on MySQL request throwing IllegalStateException in JSP 最佳做法response.getOutputStream - best practice response.getOutputStream IllegalStateException:已经为此响应调用了 getOutputStream() - IllegalStateException: getOutputStream() has already been called for this response 确保已收到`response.getOutputStream()。write()` - make sure `response.getOutputStream().write()` was received IllegalStateException:检索图像时已对此响应调用getOutputStream() - IllegalStateException: getOutputStream() has already been called for this response on retrieving image HttpsCookieFilter-IllegalStateException:此响应已调用getOutputStream() - HttpsCookieFilter - 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 调用response.getOutputStream()之后,Jasper Reports servlet停止工作。 - The Jasper Reports servlet stopped working after calling response.getOutputStream()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM