玲玲酱学习java的奇妙之旅 2022-01-13 03:24 采纳率: 77.8%
浏览 173
已结题

Springboot框架报getOutputStream() has already been called for this response

问题:直接用后台代码做了页面跳转(没有前端代码)。想实现效果:页面请求跳转的同时,服务器返回给页面cookie与uuid。
1.java代码:

@Controller
@RequestMapping("/acc")
public class beifen {
    @AccessLimit(seconds = 5, maxCount = 2)//访问控制,5秒内只能访问2次
    @ResponseBody
    @GetMapping
    public Result access(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getRequestDispatcher("/static/wjupload.html").forward(request, response);
        doGet(request,response);
        return new Result(true, StatusCode.OK, "成功");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        boolean a = response.isCommitted();
        System.out.println(a);
        //创建cookie
        try {
            UUIDUtils uuidUtils = new UUIDUtils();
            String getuuid = uuidUtils.getId();
            response.setContentType("text/html;charset=uft-8");
            PrintWriter pw = response.getWriter();
            //当用户访问该servlet时, 就将信息创建到该用户的cookie中
            //1. 现在服务器端创建一个cookie
            Cookie myCookie = new Cookie("getuuid", getuuid);
            myCookie.setPath("/");
            //2. 该cookie存在的时间 以秒为单位
            myCookie.setMaxAge(30000);
            //如果你不设置存在时间,那么该cookie将不会保存
            //3. 将该cookie写回到客户端
            response.addCookie(myCookie);
            pw.println("已经创建了cookie");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
//        读取cookie
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            //从客户端得到所有cookie信息
            Cookie[] allCookies = request.getCookies();
            int i = 0;
            //如果allCookies不为空...
            if (allCookies != null) {
                //从中取出cookie
                for (i = 0; i < allCookies.length; i++) {
                    //依次取出
                    Cookie temp = allCookies[i];
                    if (temp.getName().equals("getuuid")) {
                        //得到cookie的值
                        String val = temp.getValue();
                        pw.println("getuuid=" + val);
                        break;
                    }
                }
                if (allCookies.length == i) {
                    pw.println("cookie 过期");
                }
            } else {
                pw.println("不存在color1这个cookie/或是过期了!");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

2.出现问题:
启动后,页面没有cookie

img


3.后端控制台报错:


java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:584)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:227)
    at com.example.xiangmu.controller.beifen.doGet(beifen.java:37)
    at com.example.xiangmu.controller.beifen.access(beifen.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1732)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
java.lang.IllegalStateException: getOutputStream() has already been called for this response

img


4.在网上找了很多相关问题,找到几个可能的原因:

   request.getRequestDispatcher("/static/wjupload.html").forward(request, response);
        doGet(request,response);
        return new Result(true, StatusCode.OK, "成功");

(1)能够导致响应已经committed的操作包括:forward, redirect, flushBuffer。
在一次响应commit之前,所有的内容输出都将写入servlet引擎的缓冲区(tomcat或
          weblogic的内容空间), 而在commit之后,上一次response向缓冲区写入的内容,将清空。

 PrintWriter pw = response.getWriter();
//使用的是springboot框架

(2)如果用了OutputStream,而web容器生成的servlet代码中有out.write(””),这个和JSP中调用的
          response.getOutputStream()冲突。out.write()这个是字符流,而response.getOutputStream()
           是字节流,你不能在同一个页面中调用多个输出流。无论先调用哪一个,在调用第二个时都会抛出
  IllegalStateException,因为在jsp中,out变量是通过response.getWriter得到的。在多个使用了
  在JSP页面做输出的时候有两种方式.一是通过JspWriter,另一个是通过OutputStream,但二者互相排
斥.如果并存的话就会报告以上异常.                                         

展开全部

  • 写回答

2条回答 默认 最新

  • 关注

    问题已解决:

    package com.example.xiangmu.controller;
    import com.example.xiangmu.jiekouUtil.AccessLimit;
    import com.example.xiangmu.util.UUIDUtils;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @RestController
    @RequestMapping("/access")
    public class AccessController {
        @AccessLimit(seconds = 5, maxCount = 2)//访问控制,5秒内只能访问2次
        @GetMapping
        public String access(HttpServletRequest request, HttpServletResponse response) throws Exception {
            //创建cookie
            try{
                UUIDUtils uuidUtils=new UUIDUtils();
                String getuuid=uuidUtils.getId();
                response.setContentType("text/html;charset=utf8");
    //            PrintWriter pw = response.getWriter();
                ServletOutputStream pw= response.getOutputStream();
                //当用户访问该servlet时, 就将信息创建到该用户的cookie中
                //1. 现在服务器端创建一个cookie
                Cookie myCookie = new Cookie("getuuid", getuuid);
                //2. 该cookie存在的时间 以秒为单位
                myCookie.setMaxAge(30000);
                myCookie.setPath("/access");
                //如果你不设置存在时间,那么该cookie将不会保存
                //3. 将该cookie写回到客户端
                response.addCookie(myCookie);
            } catch(Exception ex) {
                ex.printStackTrace();
            }
    //        读取cookie
            try {
                response.setContentType("text/html;charset=utf8");
                ServletOutputStream pw= response.getOutputStream();
                //从客户端得到所有cookie信息
                Cookie[] allCookies = request.getCookies();
                Boolean flag=true;
                int i = 0;
                //如果allCookies不为空...
                if (allCookies != null) {
                    //从中取出cookie
                    for (i = 0; i < allCookies.length; i++) {
                        //依次取出
                        Cookie temp = allCookies[i];
                        System.out.println(temp.getName());
                        if (temp.getName().equals("getuuid")) {
                            //得到cookie的值
                            String val = temp.getValue();
                            System.out.println("getuuid=" + val);
                            flag=false;
                            break;
                        }
                    }
    
                    if (flag){
                        System.out.println("已经创建了cookie");
                    }
                    if (allCookies.length == i) {
                        pw.println("cookie 过期");
                    }
                } else {
                    pw.println("不存在color1这个cookie/或是过期了!");
                }
    
            } catch(Exception ex) {
                ex.printStackTrace();
            }
    
            request.getRequestDispatcher("/static/wjupload.html").forward(request, response);
            return "/static/wjupload.html";
        }
    
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 1月20日
  • 已采纳回答 1月13日
  • 创建了问题 1月13日

悬赏问题

  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
  • ¥15 一道以太网数据传输题
  • ¥15 python 下载群辉文件
  • ¥50 代码还没怎么运行但是需要代码功能调用数据
  • ¥15 vue请求不到数据,返回状态200,数据为html