oyj7454103 2012-11-12 10:26 采纳率: 100%
浏览 2230
已采纳

经典的错误,求方案。getOutputStream() has already been called for this response

[code="java"]严重: Servlet.service() for servlet spring-mvc threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response[/code]

[code="java"]
严重: Servlet.service() for servlet spring-mvc threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:611)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:180)[/code]

尝试了网上和多的解决方案 无效!

  • 写回答

7条回答 默认 最新

  • iteye_11593 2012-11-12 17:03
    关注

    下面的代码是在实现HTTP协议封装Response时的部分代码
    SimpleHttpResponse.java
    [code="java"]
    OutputStream ops;
    PrintWriter writer;
    String ce = "utf-8";
    public OutputStream getOutputStream() throws Exception {
    if(dateType==W)throw new Exception("getWriter() has already been called!");
    if(ops==null){
    ops = new ByteArrayOutputStream();
    dateType = O;
    }
    return ops;
    }
    public PrintWriter getWriter() throws Exception {
    if(dateType==O)throw new Exception("getOutputStream() has already been called!");
    if(writer==null){
    ops = new ByteArrayOutputStream();
    writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(ops,ce)),true);
    dateType = W;
    }
    return writer;
    }
    [/code]
    W和O表示已经定义流的模式。
    为什么getWriter()之后不能getOutputStream()?或者getOutputStream()之后不能getWriter()呢?
    很简单writer是写入字符的,一行写完之后还可能会插入换行回车。显然是不能和outputStream重叠使用。
    原理已经给出了,请楼主自己根据实际情况理解错误的原因。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?