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);
%>
 

 

 

你可能感兴趣的:(OutputStream)