栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java.lang.IllegalStateException: getOutputStream() has already been called for this response

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java.lang.IllegalStateException: getOutputStream() has already been called for this response

1.springboot 提供下载excel接口(通过浏览器下载)

控制台报错:

下载文件成功:

2.网上解答:

response.getOutputStream()和 response.getWriter() 两个方法不能同时使用。

建议加上:

response.reset();
out.close();

但是我的代码中都有该方法,调用接口,还是报错。

private static void setBrowser(HttpServletResponse response, HSSFWorkbook workbook, String fileName) {
        OutputStream out = null;
        try {
            //清空response
            response.reset();
            //设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            out = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/vnd.ms-excel;charset=gbk");
            //将excel写入到输出流中
            workbook.write(out);
            out.flush();
            out.close();
            response.flushBuffer();
            log.info("浏览器下载成功:{}", fileName);
        } catch (Exception e) {
            log.info("浏览器下载失败:{}", fileName);
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                    log.error("浏览器下载失败 ::: " + e.getMessage(), e);
                }
            }
        }

    }
3.我的解决方案

找到下载功能的Java代码,controller 方法中有返回值,这就是病根

 改成:

 下载成功:

 控制台打印:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/959468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号