亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

getOutputStream()異常的原因和解決方法

系統 2056 0

getOutputStream()異常的原因和解決方法

tomcat5下jsp出現getOutputStream() has already been called for this response異常的原因和解決方法

在tomcat5下jsp中出現此錯誤一般都是在jsp中使用了輸出流(如輸出圖片驗證碼,文件下載等),
沒有妥善處理好的原因。

具體的原因就是
在tomcat中jsp編譯成servlet之后在函數_jspService(HttpServletRequest request, HttpServletResponse response)的最后
有一段這樣的代碼
finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
這里是在釋放在jsp中使用的對象,會調用response.getWriter(),因為這個方法是和
response.getOutputStream()相沖突的!所以會出現以上這個異常。

然后當然是要提出解決的辦法,其實挺簡單的(并不是和某些朋友說的那樣--
將jsp內的所有空格和回車符號所有都刪除掉),

在使用完輸出流以后調用以下兩行代碼即可:

Java代碼
  1. out.clear();
  2. out=pageContext.pushBody();


最后這里是一個輸出彩色驗證碼例子(這樣的例子幾乎隨處可見)
imag.jsp


Java代碼
  1. <%@page import = "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
  2. <%@page import = "java.io.OutputStream" %>
  3. <%!
  4. ColorgetRandColor( int fc, int bc){
  5. Randomrandom= new Random();
  6. if (fc> 255 )fc= 255 ;
  7. if (bc> 255 )bc= 255 ;
  8. int r=fc+random.nextInt(bc-fc);
  9. int g=fc+random.nextInt(bc-fc);
  10. int b=fc+random.nextInt(bc-fc);
  11. return new Color(r,g,b);
  12. }
  13. %>
  14. <%
  15. try {
  16. response.setHeader( "Pragma" , "No-cache" );
  17. response.setHeader( "Cache-Control" , "no-cache" );
  18. response.setDateHeader( "Expires" , 0 );
  19. int width= 60 ,height= 20 ;
  20. BufferedImageimage= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  21. OutputStreamos=response.getOutputStream();
  22. Graphicsg=image.getGraphics();
  23. Randomrandom= new Random();
  24. g.setColor(getRandColor( 200 , 250 ));
  25. g.fillRect( 0 , 0 ,width,height);
  26. g.setFont( new Font( "TimesNewRoman" ,Font.PLAIN, 18 ));
  27. g.setColor(getRandColor( 160 , 200 ));
  28. for ( int i= 0 ;i< 155 ;i++)
  29. {
  30. int x=random.nextInt(width);
  31. int y=random.nextInt(height);
  32. int xl=random.nextInt( 12 );
  33. int yl=random.nextInt( 12 );
  34. g.drawLine(x,y,x+xl,y+yl);
  35. }
  36. StringsRand= "" ;
  37. for ( int i= 0 ;i< 4 ;i++){
  38. Stringrand=String.valueOf(random.nextInt( 10 ));
  39. sRand+=rand;
  40. g.setColor( new Color( 20 +random.nextInt( 110 ), 20 +random.nextInt( 110 ), 20 +random.nextInt( 110 )));
  41. g.drawString(rand, 13 *i+ 6 , 16 );
  42. }
  43. session.setAttribute( "rand" ,sRand);
  44. g.dispose();
  45. ImageIO.write(image, "JPEG" ,os);
  46. os.flush();
  47. os.close();
  48. os= null ;
  49. response.flushBuffer();
  50. out.clear();
  51. out=pageContext.pushBody();
  52. }
  53. catch (IllegalStateExceptione)
  54. {
  55. System.out.println(e.getMessage());
  56. e.printStackTrace();
  57. }%>

來自:http://jessdy.javaeye.com/blog/187448

  1. 原始代碼:
  2. <%@pagecontentType= "text/html;charset=GBK" %>
  3. <%@pageautoFlush= "false" %>
  4. <%@page import = "java.sql.*" %>
  5. <%@page import = "java.io.*" %>
  6. <%@page import = "java.util.*" %>
  7. <%@page import = "java.awt.*" %>
  8. <%@page import = "java.awt.image.*" %>
  9. <%@page import = "com.sun.image.codec.jpeg.*" %>
  10. <%@page import = "com.sun.image.codec.jpeg.*" %>
  11. <%@page import = "com.csland.common.util.*" %>
  12. <%!
  13. public void noImg(ServletOutputStreamoutStream){
  14. try {
  15. int width= 80 ,height= 50 ;
  16. BufferedImageimage= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  17. Graphicsg=image.getGraphics();
  18. //以下填充背景顏色
  19. g.setColor(Color.WHITE);
  20. g.fillRect( 0 , 0 ,width,height);
  21. g.setColor(Color.ORANGE);
  22. g.drawRect( 0 , 0 ,width- 1 ,height- 1 );
  23. Stringrandom= "random" ;
  24. //以下設置前景色
  25. g.setColor(Color.BLACK);
  26. g.drawString( "沒有簽名圖" , 0 , 25 );
  27. g.dispose();
  28. //
  29. JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(outStream);
  30. encoder.encode(image);
  31. outStream.flush();
  32. //outStream.close();
  33. } catch (Exceptione){
  34. Log.fetal( "atnoImginsig2.jsp:/n" +e);
  35. }
  36. }
  37. public void noImg(HttpServletResponseresponse){
  38. try {
  39. ServletOutputStreamoutStream=response.getOutputStream();
  40. noImg(outStream);
  41. } catch (Exceptione){
  42. Log.fetal( "atnoImginsig2.jsp:/n" +e);
  43. }
  44. }
  45. %>
  46. <%
  47. out.clear();
  48. response.setContentType( "image/jpeg" );
  49. response.addHeader( "pragma" , "NO-cache" );
  50. response.addHeader( "Cache-Control" , "no-cache" );
  51. response.addDateHeader( "Expries" , 0 );
  52. int id=StrUtil.StrToInt(request.getParameter( "id" ));
  53. if (id<= 0 )
  54. {
  55. noImg(response);
  56. }
  57. else
  58. {
  59. java.sql.Connectionconn= null ;
  60. java.sql.Statementstmt= null ;
  61. java.sql.ResultSetlobDetails= null ;
  62. try {
  63. conn=DBUtil.getConn();
  64. stmt=conn.createStatement();
  65. lobDetails=stmt.executeQuery(
  66. "SELECTSignatureFROMOZ_Member" +
  67. "WHEREMemberID=" +id+ "" );
  68. if (lobDetails.next()){
  69. BlobmapBlob=lobDetails.getBlob( 1 );
  70. if (mapBlob!= null ){
  71. InputStreamblobStream=mapBlob.getBinaryStream();
  72. Log.debug( "blobStream:" +blobStream);
  73. ServletOutputStreamoutStream=response.getOutputStream();
  74. byte []buffer= new byte [ 10 * 1024 ];
  75. int nbytes= 0 ;
  76. int allBytes= 0 ;
  77. while ((nbytes=blobStream.read(buffer))!=- 1 ){
  78. outStream.write(buffer, 0 ,nbytes);
  79. allBytes+=nbytes;
  80. }
  81. Log.debug( "allBytes:" +allBytes);
  82. if (allBytes< 1 ){
  83. Log.debug( "allBytes2:" +allBytes);
  84. noImg(outStream);
  85. } else {
  86. outStream.flush();
  87. //outStream.close();
  88. }
  89. blobStream.close();
  90. } else {
  91. noImg(response);
  92. }
  93. } else {
  94. noImg(response);
  95. }
  96. }
  97. catch (Exceptione){
  98. Log.fetal(e);
  99. }
  100. finally {
  101. try {lobDetails.close();} catch (Exceptione){}
  102. try {stmt.close();} catch (Exceptione){}
  103. try {conn.close();} catch (Exceptione){}
  104. }
  105. }
  106. %>
  107. 修改后的代碼===============byyanleigis
  108. <%@pagecontentType= "text/html;charset=GBK" %>
  109. <%@pageautoFlush= "false" %>
  110. <%@page import = "java.sql.*" %>
  111. <%@page import = "java.io.*" %>
  112. <%@page import = "java.util.*" %>
  113. <%@page import = "java.awt.*" %>
  114. <%@page import = "java.awt.image.*" %>
  115. <%@page import = "com.sun.image.codec.jpeg.*" %>
  116. <%@page import = "com.sun.image.codec.jpeg.*" %>
  117. <%@page import = "com.csland.common.util.*" %>
  118. <%!
  119. public void noImg(ServletOutputStreamoutStream){
  120. try {
  121. int width= 80 ,height= 50 ;
  122. BufferedImageimage= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  123. Graphicsg=image.getGraphics();
  124. //以下填充背景顏色
  125. g.setColor(Color.WHITE);
  126. g.fillRect( 0 , 0 ,width,height);
  127. g.setColor(Color.ORANGE);
  128. g.drawRect( 0 , 0 ,width- 1 ,height- 1 );
  129. Stringrandom= "random" ;
  130. //以下設置前景色
  131. g.setColor(Color.BLACK);
  132. g.drawString( "沒有簽名圖" , 0 , 25 );
  133. g.dispose();
  134. //
  135. JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(outStream);
  136. encoder.encode(image);
  137. outStream.flush();
  138. outStream.close();
  139. } catch (Exceptione){
  140. Log.fetal( "atnoImginsig2.jsp:/n" +e);
  141. }
  142. }
  143. public void noImg(HttpServletResponseresponse){
  144. try {
  145. ServletOutputStreamoutStream=response.getOutputStream();
  146. noImg(outStream);
  147. } catch (Exceptione){
  148. Log.fetal( "atnoImginsig2.jsp:/n" +e);
  149. }
  150. }
  151. %>
  152. <%
  153. out.clear();
  154. out=pageContext.pushBody();
  155. response.setContentType( "image/jpeg" );
  156. response.addHeader( "pragma" , "NO-cache" );
  157. response.addHeader( "Cache-Control" , "no-cache" );
  158. response.addDateHeader( "Expries" , 0 );
  159. int id=StrUtil.StrToInt(request.getParameter( "id" ));
  160. if (id<= 0 )
  161. {
  162. noImg(response);
  163. }
  164. else
  165. {
  166. java.sql.Connectionconn= null ;
  167. java.sql.Statementstmt= null ;
  168. java.sql.ResultSetlobDetails= null ;
  169. try {
  170. conn=DBUtil.getConn();
  171. stmt=conn.createStatement();
  172. lobDetails=stmt.executeQuery(
  173. "SELECTSignatureFROMOZ_Member" +
  174. "WHEREMemberID=" +id+ "" );
  175. if (lobDetails.next()){
  176. BlobmapBlob=lobDetails.getBlob( 1 );
  177. if (mapBlob!= null ){
  178. InputStreamblobStream=mapBlob.getBinaryStream();
  179. Log.debug( "blobStream:" +blobStream);
  180. ServletOutputStreamoutStream=response.getOutputStream();
  181. byte []buffer= new byte [ 10 * 1024 ];
  182. int nbytes= 0 ;
  183. int allBytes= 0 ;
  184. while ((nbytes=blobStream.read(buffer))!=- 1 ){
  185. outStream.write(buffer, 0 ,nbytes);
  186. allBytes+=nbytes;
  187. }
  188. Log.debug( "allBytes:" +allBytes);
  189. if (allBytes< 1 ){
  190. Log.debug( "allBytes2:" +allBytes);
  191. noImg(outStream);
  192. } else {
  193. outStream.flush();
  194. //outStream.close();
  195. }
  196. blobStream.close();
  197. } else {
  198. noImg(response);
  199. }
  200. } else {
  201. noImg(response);
  202. }
  203. response.flushBuffer();
  204. out.clear();
  205. out=pageContext.pushBody();
  206. }
  207. catch (Exceptione){
  208. Log.fetal(e);
  209. }
  210. finally {
  211. try {lobDetails.close();} catch (Exceptione){}
  212. try {stmt.close();} catch (Exceptione){}
  213. try {conn.close();} catch (Exceptione){}
  214. }
  215. }
  216. %>

getOutputStream()異常的原因和解決方法


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲成在人线久久综合 | 神马九九 | 成人欧美一区二区三区黑人免费 | 亚洲欧美日韩国产 | 国产午夜成人无码免费看 | 青青热久久国产久精品 | 97影院理论 | 91资源视频 | 久久一日本道色综合久久m 久久一色本道亚洲 | 五月天丁香婷婷综合久久 | 国产美女久久久久久久久久久 | 日韩不卡在线观看 | 国产亚洲一区二区在线观看 | 亚洲欧美一区二区三区综合 | 草草免费视频 | 久在线观看 | 97se亚洲综合| 久久国产精品一区二区三区 | 99视频在线观看免费视频 | 久青草免费在线视频 | 奇米99| 一区二区不卡久久精品 | 麻豆成人久久精品二区三区小说 | 久操免费在线观看 | 亚洲欧美日韩综合一区久久 | 黄视频网站观看 | 久久99久久99精品免观看不卡 | 久久天天躁狠狠躁夜夜爽 | 自拍 欧美 在线 综合 另类 | 精品视频久久 | 欧美日韩亚洲国产精品一区二区 | 国产精品视频色拍拍 | 综合网视频 | 狠狠操亚洲 | 精品久久久久久亚洲精品 | 久久精品阿娇 | 久草网在线观看 | 国产精品一区二区三区久久 | 中文在线免费视频 | 午夜免费福利视频 | 成年女人毛片免费观看中文w |