getOutputStream() has already bean called for this response

作者:刘庆

@version 1.0

@see 有很多同事一直都问我Servelt OutputStream 流的问题,在这里给大家一个解释为什么会这样呢

 

 

2012-4-4 23:13:46 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [/reportsystem] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
 

 

1) Jsp 和Servlet 原理介绍

Jsp 在解释成Servlet 时

 

response.setContentType("image/jpeg; charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      //注意思这时的out 上JspWriter
      //是通过response 获得的
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
   //这是我们加的代码
    OutputStream output = response.getOutputStream();
    RandomImage randomImage = new RandomImage();
    randomImage.getImage(output);

 

这样一调用自然就会有getOutputStream() has already been called for this response 导常

 

这是为什么呢

简单的理解就是:前面张三吃的一碗面条是牛肉面。碗没有洗,又用这碗给你装了一碗排骨面,你说你会不会

告诉老板,老板你这碗没洗,但是面还是可以吃对吧。。

所以要解决这个问题,,我们洗碗就可以了对吧

 

<%@page import="org.zk.liuqing.image.RandomImage"%>
<%@page import="java.io.OutputStream"%>
<%@ page language="java" contentType="image/jpeg; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    /**
     * @author 刘庆
     * @see 洗一下碗,清空一下前面的缓存不就OK了。希望对大家有所帮助
     **/
    out.clearBuffer();
    OutputStream output = response.getOutputStream();
    RandomImage randomImage = new RandomImage();
    randomImage.getImage(output);
%>
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值