alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
31.64k stars 7.46k forks source link

导出 Excel 出现 Can not close IO #3698

Closed orcl-zung closed 5 days ago

orcl-zung commented 5 months ago

建议先去看文档

快速开始常见问题

异常代码

  @Override
    @PostMapping("/test")
    public HttpResult<String> test(@RequestParam("count") Integer count) throws Exception {
        HttpServletResponse res = getResponse();
        res.setContentType("application/force-download");
        String fileName = URLEncoder.encode("订单列表.xlsx", StandardCharsets.UTF_8.toString());
        res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        ExcelWriter excelWriter = EasyExcelFactory.write(res.getOutputStream()).build();
        try {
            List<TestVo> list = new ArrayList<>();

            File file = new File("D:\\wallpaper\\1347724.png");
            URL url = file.toURL();
            for (int i = 1; i <= count; i++) {
                TestVo testVo = new TestVo();

                testVo.setTestString("row " + i);
                testVo.setInchImage(url);
                list.add(testVo);
            }

            WriteSheet writeSheet = EasyExcelFactory.writerSheet(0, "订单").head(TestVo.class).build();
            excelWriter.write(list, writeSheet);

            return HttpResult.success();
        } catch (Exception e) {
            logger.error("列表导出 Excel 异常!", e);
            return HttpResult.failure("导出异常");
        } finally {
            excelWriter.finish();
        }
    }

    @Getter
    @Setter
    @ColumnWidth(20)
    @ContentRowHeight(120)
    @ToString
    static class TestVo {

        @ExcelProperty(value = "测试列")
        private String testString;

        @ExcelProperty(value = "图片", converter = CustomUrlImageConverter.class)
        private URL inchImage;
    }

public class CustomUrlImageConverter implements Converter<URL> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return URL.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.IMAGE;
    }

    @Override
    public URL convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
            GlobalConfiguration globalConfiguration) {
        throw new UnsupportedOperationException("Cannot convert images to url.");
    }

    @Override
    public CellData<?>  convertToExcelData(URL value, ExcelContentProperty contentProperty,
            GlobalConfiguration globalConfiguration) throws IOException {
        InputStream inputStream = null;
        try {
            if (null == value) {
                return new CellData<>();
            }
            inputStream = value.openStream();
            byte[] bytes = IoUtils.toByteArray(inputStream);
            return new CellData<>(bytes);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }
}

异常提示

企业微信截图_17085021169931 企业微信截图_8 企业微信截图_7

问题描述

异常如图 1 所示,看过源码之后发现出现异常 Can not close IO 的原因并不是相关 Issue 所描述的情况,我这边的原因是关闭流的时候会进行数组扩充,扩充时老年代被打满,一直触发 Full GC ,导致 OOM,这里不理解的是为什么关闭流的时候会进行数组扩充?

konrry commented 5 months ago

Hi, 这个问题接了吗?

orcl-zung commented 5 months ago

@konrry 还没有呢

nlrq commented 3 months ago

运行官方的下载demo,出现Can not close IO,commons-compress升级到1.19就好了

gongxuanzhang commented 2 weeks ago

你好,我们发现此Issue已经超过三个月没有活动了,为了更好的帮助您解决问题,我们将在两周后关闭此Issue,如果您仍然有问题,请在两周内内回复此Issue,谢谢!如果您已经解决或者不需要帮助,请忽略此消息。