欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

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

系統 2119 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元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲国产一区二区三区四区 | 最新日韩精品在线观看 | 四季久久免费一区二区三区四区 | 草草浮力影视 | 欧美手机看片 | 日本hdxxxxx护士免费的 | 亚洲最大成人综合 | 免费中文字幕 | 国产精品乱码人人做人人爱 | 久久99视频 | 99人中文字幕亚洲区 | 久久久精品久久视频只有精品 | 久久久www成人免费无遮挡大片 | 日日拍夜夜嗷嗷叫视频 | 91小视频在线观看免费版高清 | 日韩中文视频 | 视频一区二区在线观看 | 午夜视频在线观看视频 | 91伊人久久大香线蕉 | 亚洲黄色网址 | 黄色免费在线观看 | 精品乱子伦一区二区三区 | 国产精品永久免费视频观看 | 国产高清在线看 | 婷婷国产成人久久精品激情 | 久久久精品在线观看 | 大香一本蕉伊线亚洲网 | 亚洲美女精品 | 色久影院 | 欧洲免费无线码二区5 | 91短视频在线观看 | 阿v免费在线观看 | www夜色| 国产日韩欧美在线 | 国产欧美一级二级三级在线视频 | 伊人色综合97 | 欧美成人全部费免网站 | 国产成人精品免费午夜 | av在线播放免费 | 我和我的祖国电影在线观看免费版高清 | 免费a视频在线观看 |