[alibaba/easyexcel]读取文件的时候,ExcelReader读取隐藏的工作簿java.lang.OutOfMemoryError: Java heap space

2024-05-16 115 views
1

触发场景描述

一个Excel文件含有多个工作簿,设置部分工作簿隐藏之后,通过文件上传EasyExcel读取解析该文件出现OOM [Uploading 日报模板-0304的副本2.xlsx…]()

触发Bug的代码

    if (file == null) {
            return null;
        }
        ExcelReader excelReader = null;
        SourceJoinDataListener sourceJoinDataListener = new SourceJoinDataListener(Constant.FILE_SOURCE_ID.toString(), preview, append, tableNameIdMap);
        try (InputStream inputStream = file.getInputStream()) {
            excelReader = EasyExcelFactory.read(inputStream , sourceJoinDataListener).build();
            List<ReadSheet> readSheets = excelReader.excelExecutor().sheetList();
            checkDuplicateSheetNameAndLength(readSheets);
            excelReader.read(readSheets);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                throw e;
            }
            Throwable cause = e.getCause();
            if (cause instanceof BusinessException) {
                String message = e.getMessage();
                if (!StringUtils.isNullOrEmpty(message)) {
                    String[] split = message.split(":");
                    String errorMessage = split.length > 1 ? split[1].trim() : message;
                    ErrorCodeEnum.CUSTOMIZE_BUSINESS_EXCEPTION.throwBizException(errorMessage);
                }
            } else {
                e.printStackTrace();
                log.error("解析excel失败", e);
                ErrorCodeEnum.PARSE_EXCEL_ERROR.throwBizException();
            }
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
                excelReader.finish();
            }
        }

提示的异常或者没有达到的效果

com.alibaba.excel.exception.ExcelAnalysisException: java.lang.OutOfMemoryError: Java heap space
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:134)
    at com.alibaba.excel.ExcelReader.read(ExcelReader.java:67)
    at com.ygp.data.bi.common.utils.ExcelConvertTableUtil.parseExcelToTable(ExcelConvertTableUtil.java:60)
    at com.ygp.data.bi.service.access.FilePreviewServiceImpl.filePreview(FilePreviewServiceImpl.java:32)
    at com.ygp.data.bi.service.access.SourceMicroserviceImpl.filePreview(SourceMicroserviceImpl.java:273)
    at com.ygp.data.bi.service.access.SourceMicroserviceImpl$$FastClassBySpringCGLIB$$5ab13a41.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:687)
    at com.ygp.data.bi.service.access.SourceMicroserviceImpl$$EnhancerBySpringCGLIB$$ecf2c8b.filePreview(<generated>)
    at com.ygp.data.bi.service.access.SourceJoinMicroserviceImpl.filePreview(SourceJoinMicroserviceImpl.java:474)
    at com.ygp.data.bi.service.access.SourceJoinMicroserviceImpl$$FastClassBySpringCGLIB$$6af4bf8b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:687)
    at com.ygp.data.bi.service.access.SourceJoinMicroserviceImpl$$EnhancerBySpringCGLIB$$7a6a5aa9.filePreview(<generated>)
    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.apache.servicecomb.core.handler.impl.ProducerOperationHandler.doInvoke(ProducerOperationHandler.java:121)
    at org.apache.servicecomb.core.handler.impl.ProducerOperationHandler.syncInvoke(ProducerOperationHandler.java:106)
    at org.apache.servicecomb.core.handler.impl.ProducerOperationHandler.invoke(ProducerOperationHandler.java:60)
    at org.apache.servicecomb.core.handler.impl.ProducerOperationHandler.handle(ProducerOperationHandler.java:51)
    at org.apache.servicecomb.core.Invocation.next(Invocation.java:307)
    at com.ygp.data.common.service.handler.traceSpan.ProviderTraceSpanHandler.handle(ProviderTraceSpanHandler.java:23)
    at org.apache.servicecomb.core.Invocation.next(Invocation.java:307)
    at org.apache.servicecomb.common.rest.AbstractRestInvocation.doInvoke(AbstractRestInvocation.java:245)
    at org.apache.servicecomb.common.rest.AbstractRestInvocation.invoke(AbstractRestInvocation.java:219)
    at org.apache.servicecomb.common.rest.AbstractRestInvocation.runOnExecutor(AbstractRestInvocation.java:203)
    at org.apache.servicecomb.common.rest.AbstractRestInvocation.lambda$scheduleInvocation$0(AbstractRestInvocation.java:162)
    at com.ygp.data.common.framework.trace.async.TraceRunnable.run(TraceRunnable.java:45)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3210)
    at java.util.Arrays.copyOf(Arrays.java:3181)
    at java.util.ArrayList.grow(ArrayList.java:267)
    at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:241)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:233)
    at java.util.ArrayList.add(ArrayList.java:464)
    at com.ygp.data.bi.common.excel.SourceJoinDataListener.handlerDataType(SourceJoinDataListener.java:121)
    at com.ygp.data.bi.common.excel.SourceJoinDataListener.invoke(SourceJoinDataListener.java:97)
    at com.ygp.data.bi.common.excel.SourceJoinDataListener.invoke(SourceJoinDataListener.java:35)
    at com.alibaba.excel.read.processor.DefaultAnalysisEventProcessor.dealData(DefaultAnalysisEventProcessor.java:99)
    at com.alibaba.excel.read.processor.DefaultAnalysisEventProcessor.endRow(DefaultAnalysisEventProcessor.java:46)
    at com.alibaba.excel.analysis.v07.handlers.RowTagHandler.endElement(RowTagHandler.java:46)
    at com.alibaba.excel.analysis.v07.handlers.sax.XlsxRowHandler.endElement(XlsxRowHandler.java:91)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:610)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1784)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2969)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:507)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:867)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:796)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:142)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1216)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:644)
    at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.parseXmlSource(XlsxSaxAnalyser.java:197)
    at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.execute(XlsxSaxAnalyser.java:218)
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:123)
    at com.alibaba.excel.ExcelReader.read(ExcelReader.java:67)
    at com.ygp.data.bi.common.utils.ExcelConvertTableUtil.parseExcelToTable(ExcelConvertTableUtil.java:60)
    at com.ygp.data.bi.service.access.FilePreviewServiceImpl.filePreview(FilePreviewServiceImpl.java:32)
    at com.ygp.data.bi.service.access.SourceMicroserviceImpl.filePreview(SourceMicroserviceImpl.java:273)
    at com.ygp.data.bi.service.access.SourceMicroserviceImpl$$FastClassBySpringCGLIB$$5ab13a41.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

回答

6

2.0.+和3.0+的easy excel都会有这个问题 , 不论是07还是03版本的excel文件

我的版本号:implementation "com.alibaba:easyexcel:3.0.5"

1

你能SourceJoinDataListener 代码也填写下么?我直接

    List<Object> list =
            EasyExcel.read(new FileInputStream("/Users/zhuangjiaju/Downloads/-0304.2.xlsx")).useDefaultListener(false).doReadAllSync();
        for (Object data : list) {
            LOGGER.info("返回数据:{}", JSON.toJSONString(data));
        }

这么跑没问题

2

这个问题是否已经解决?未解决请重新打开