日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 2239 0

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

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

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

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

然后當(dāng)然是要提出解決的辦法,其實(shí)挺簡(jiǎn)單的(并不是和某些朋友說的那樣--
將jsp內(nèi)的所有空格和回車符號(hào)所有都刪除掉),

在使用完輸出流以后調(diào)用以下兩行代碼即可:

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


最后這里是一個(gè)輸出彩色驗(yàn)證碼例子(這樣的例子幾乎隨處可見)
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. //以下設(shè)置前景色
  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. //以下設(shè)置前景色
  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()異常的原因和解決方法


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 晋州市| 满洲里市| 宁夏| 盖州市| 昭觉县| 金乡县| 兴山县| 潮州市| 社旗县| 县级市| 科尔| 乌苏市| 大理市| 都安| 龙陵县| 施甸县| 资中县| 常宁市| 宝应县| 浦城县| 和林格尔县| 乡宁县| 陆河县| 同江市| 沅江市| 手机| 凤阳县| 牟定县| 佛教| 宁国市| 石河子市| 丽水市| 内乡县| 鲁山县| 叙永县| 靖西县| 新巴尔虎左旗| 晋江市| 平湖市| 南涧| 贡嘎县|