古之成大事者,不唯有超世之才,亦唯有堅韌不拔之志也!

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            3 隨筆 :: 44 文章 :: 1 評論 :: 0 Trackbacks
          getOutputStream() has already been called for this response問題的解決

          在jsp向頁面輸出圖片的時候,使用response.getOutputStream()會有這樣的提示:java.lang.IllegalStateException:getOutputStream() has already been called for this response,會拋出Exception
          原因一:
           JSP 默認的輸出流為PrintWriter ,即<% %>以外的東西所默認的輸出方式,如果你嘗試在JSP中使用ServletOutputStream就會引起錯誤.要嘛直接改用Servlet輸出 (復寫service方法),要嘛刪除除%><%中的任何東西(包括HTML標簽,空格,回車等東西)應該就可以。
          對于這樣的情況應該這樣來解決,刪除%><%之間的所有內容包括空格和換行符,最后也要消除空格和換行符,最好再加上一句response.reset()。
          原因二:
               
          在J2EE的API參考里有這么個:

          ServletResponse的getWriter()方法里會拋出這個異常,

          IllegalStateException - if the getOutputStream method has already been called
          for this response object

          而它的getOutputStream()方法里會拋出這個異常.

          IllegalStateException - if the getOutputStream method has already been called for this response object

          并且兩者的函數申明里都有這么樣的一句
          Either this method or getOutputStream() may be called to write the body, not both.
          Either this method or getWriter() may be called to write the body, not both.

           以上說明也解釋了為什么在往頁面中寫入圖片的時候要使用如下循環格式
          OutputStream output=response.getOutputStream();
           while((len=in.read(b)) >0)
            {
            output.write(b,0,len);  
           
            }
           output.flush();
          而不是把response.getOutputStream().write()放到循環體內
          posted on 2008-05-16 11:00 goto 閱讀(261) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 奇台县| 漳浦县| 舟曲县| 阿克苏市| 历史| 普定县| 桦甸市| 错那县| 伊宁市| 革吉县| 敦煌市| 永胜县| 探索| 应用必备| 奎屯市| 贵州省| 曲水县| 永修县| 休宁县| 新宾| 札达县| 长宁区| 宝坻区| 防城港市| 望奎县| 吉首市| 四子王旗| 开江县| 阳高县| 孝感市| 永胜县| 英吉沙县| 新昌县| 云梦县| 临邑县| 泸溪县| 万载县| 铜陵市| 密山市| 崇礼县| 淮南市|