getOutputStream()异常的原因和解决方法

系统 1841 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条评论