2013-03-26 115 views
0

我試圖從MySQL-DB下載PDF文件(我把它作爲一個斑點,併到這一點,一切都運行得很好。但是,當我的時候試圖讓ServletOutputStream的將它發送給客戶端的PROGRAMM崩潰。

AFAIK的異常被拋出時,方法被調用或的getWriter法面前,但是我沒有在我的代碼中使用的getWriter法在所有和在PROGRAMM其他的getOutputStream的方法都沒有達到(我評論他們出去,以確保)。
(整個事情發生在JSP,如果該事項因任何原因)

我的繼承人代碼snippe t和例外:response.getOutputStream()拋出IllegalStateException異常稱爲首次

代碼

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) 

回答

2

請檢查您的回覆是否已提交。如果您嘗試對承諾的響應進行操作,則可能會出現IllegalStateException。如果頁面緩衝區大小超出,響應將被提交。

+0

你說得對,就是這個問題。但是由於響應在JSP的開始已經提交,甚至在甚至改變了任何內容或者在響應緩衝區中寫入了任何內容之前,我怎麼能改變它呢? – olkoza 2013-03-26 09:31:17

+0

看到我上面的答案。 – NilsH 2013-03-26 11:43:19

+0

但這怎麼可能?甚至當我更改代碼以最小的是: '<%@頁\t進口= 「com.nundp.util.ResourceHandler,java.util中*,java.io *,java.sql.Blob中的」 % > <%的System.out.println( 「開始的jsp:」 + response.isCommitted());%>' 響應已經COMMITED? – olkoza 2013-03-26 14:55:52

1

它可能確實事,你在JSP這樣做。請記住,JSP是Web應用程序請求的輸出呈現階段的一部分,因此容器可能已經獲取了writer對象。嘗試將代碼轉移到servlet中。它會給你更多的控制作家/流。