gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:55:39 27 4
gpt4 key购买 nike

我正在尝试从 MySQL-DB 下载 pdf(我将其作为 Blob 获取,到目前为止一切正常。但是当我尝试让 ServletOutputstream 将其发送到客户端时,程序崩溃。

AFAIK 当该方法之前被调用或 getwriter-method 被调用时,会抛出异常。但我根本不在代码中使用 getwriter 方法,并且程序中的其他 getOutPutStream 方法未达到(我将它们注释掉以确保)。
(如果出于某种原因,整个事情都发生在 JSP 中)

这是我的代码片段和异常:

代码

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

异常

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)

最佳答案

请检查您的回复是否已提交。如果您尝试对已提交的响应执行某些操作,则可能会出现 IllegalStateException。如果超出页面缓冲区大小,将提交响应。

关于java - 第一次调用response.getOutputStream()时抛出IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15632611/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com