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

            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
          主站蜘蛛池模板: 南召县| 拉孜县| 丰宁| 哈尔滨市| 奉贤区| 德钦县| 襄樊市| 延安市| 临西县| 阿鲁科尔沁旗| 茶陵县| 景谷| 玉龙| 武安市| 香河县| 哈尔滨市| 岳西县| 宁都县| 高阳县| 西吉县| 新民市| 介休市| 四会市| 寻乌县| 鄂温| 安福县| 法库县| 建始县| 肥东县| 彝良县| 交口县| 营山县| 微博| 海阳市| 鲁甸县| 绿春县| 三江| 芮城县| 大田县| 曲阜市| 余庆县|