【已解决】使用easy-poi实现导入excel功能,报错 getWriter() has already been called for this response] with root cause

@[TOC]( [已解决]getWriter() has already been called for this response] with root cause)

【已解决】使用easy-poi实现导入excel功能,报错 getWriter() has already been called for this response] with root cause,影响导出excel

在项目中,需要导出查询数据为excel,在本地使用idea测试没有问题。但在测试环境,相同代码,报错“getWriter() has already been called for this response] with root cause”,影响下载功能。
另外,报错 “getWriter() has already been called for this response] with root cause” 的问题已经找到原因,并解决,请看文章最后一段。

报错截图

在这里插入图片描述

分析报错

通过对报错信息的分析,关键就是流信息重复开

解决方法

原来代码

private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws Exception {
        OutputStream outputStream=null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            //response.flushBuffer();
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            throw new ServiceException(ResponseEnum.EXCEPTION_FILE_IO);
        }finally {
            try {
                if(outputStream!=null){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

修改后的代码

private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws Exception {
        OutputStream outputStream=null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            //-------------重点------------
            response.flushBuffer();
            //--------------重点----------
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            throw new ServiceException(ResponseEnum.EXCEPTION_FILE_IO);
        }finally {
            try {
                if(outputStream!=null){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

请看注释"-----重点--------"部分

报错问题已解决

原因

log.info("### Request Args   : {}", JacksonUtil.marshallToString(joinPoint.getArgs()));

在controller入参前和出参,都有做日志的打印,但是,是使用JacksonUtil.marshallToString,来解析的,再此解析过程中,会有报错“getWriter() has already been called for this response”产生

解决方案

通过判断,如果是导出或导入,则不做日志的拦截打印。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值