`
liss
  • 浏览: 826813 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

getWriter() has already been called for this response的解决办法

阅读更多
Servlet规范说明,不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException


对于getOutputStream(),api里是这样说的:Either this method or getWriter() may be called to write the body, not both.

也就是getOutputStream()和getWriter() 只能使用一个,把代码里的response.getOutputStream()全部删之后,统一使用response.getWriter() 作为输出流,问题解决。

全部代码如下


response.setContentType( "application/octet-stream ");
        response.addHeader("Content-Disposition","attachment;filename=" + "work.xls");
        java.io.FileInputStream fileInputStream = null;
        ServletOutputStream sos = null;
        PrintWriter writer = null;
        try
        {
            fileInputStream = new java.io.FileInputStream(file);
            writer = response.getWriter();
            response.getOutputStream();
            int i;
            while((i = fileInputStream.read()) != -1)
            {
                writer.write(i);
            }
            writer.flush();
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            if(sos != null)
            {
                try
                {
                    sos.close();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
            if(fileInputStream != null)
            {
                try
                {
                    fileInputStream.close();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
            if(writer != null)
            {
                writer.close();
            }
        }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yuzhongzhu/archive/2011/03/31/6290526.aspx#
分享到:
评论

相关推荐

    powerbuilder

    从excel导入数据到datawindow-转载

    response.getWriter().write()用于ajax

    NULL 博文链接:https://yuhuiblog695685688425687986842568269.iteye.com/blog/2336132

    response.getWriter().write()向前台打印信息乱码问题解决

    本节主要介绍了response.getWriter().write()向前台打印信息乱码问题解决方法,需要的朋友可以参考下

    servlet2.4doc

    Returns a boolean indicating whether the named response header has already been set. contextDestroyed(ServletContextEvent) - Method in interface javax.servlet.ServletContextListener Notification ...

    基于JAVa的网上商城项目完整源码.zip

    * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...

    网上书城 购物系统 jsp

    * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...

    java一个论坛的源码

    * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...

    网上购物系统

    * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to ...

    Servelt技术做的E家园

    * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the ...

    JavaWeb教材配套资源

    JavaWeb教材配套资源,内含每个章节的Demo项目源码。包含教材最后完整的网上商城案例源码,包含sql及项目源码... PrintWriter out = response.getWriter(); out.print("this servlet is created by eclipse"); } }

    医院管理系统.rar

    * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server ...

    【servlet】彻底解决doGet、doPost以及控制台中文乱码问题

    PrintWriter out = response.getWriter(); out.println(你好,世界!); 前端显示: 1.2 原因 没有设置HttpServletResponse使用哪种编码,默认编码跟浏览器解码不匹配。 1.3 解决方案: 方案1: 在PrintWriter out ...

    实践考核类课二 选课系统

    * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to ...

    servlet中文乱码问题

    Servlet + Tomcat 中文乱码的解决方法,浏览器把Unicode字符转换为GBK字符,这样页面的内容和浏览器的显示模式都设成了GBK,就不会乱码了

    ajax树形展示,JavaScript

    java.io.PrintWriter out = response.getWriter(); for(int i= 0;i;i++)//循环显示每个分类 out.println(treeviewRender.renderTreeViewAjax(top[i],false)); } } public void doGet...

    jsp网上投票系统myeclipse开发

    * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server ...

    jQuerySlide切换

    PrintWriter out = response.getWriter(); AdvertDao adverDao=new AdvertDao(); List<Advert> list=adverDao.getAdverts(); out.write(JSONArray.fromObject(list).toString());//把数据转换成...

    java web的注意事项

    getWriter() 和Response.getOutputStream冲突

    HttpServletResponse

    PrintWriter getWriter() 获得字符流,通过字符流的write(String s)方法可以将字符串设置到response 缓冲区中,随后Tomcat会将response缓冲区中的内容组装成Http响应返回给浏览 器端。 文件下载的实质就是文件拷贝...

Global site tag (gtag.js) - Google Analytics