第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Java解析Excel遇見的問題

標(biāo)簽:
Java
项目中使用java解析excel经常会遇见oom的问题,这里选择两个开源框架,进行简单的比较和测试。

EasyExcel

阿里开源(EasyExcel)
引入maven依赖
       <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.2-beta5</version>
        </dependency>
        
使用EasyExcel对我们解析excel提供了便利,但是测试的时候当excel达到100M左右
依然会发送OOM,下面看一下入门的使用方法

简单使用

public class ExcelListener extends AnalysisEventListener {

    private List<Object> datas = new ArrayList<Object>();
    public void invoke(Object object, AnalysisContext context) {
        System.out.println("当前行:"+context.getCurrentRowNum());
        System.out.println(object);
        datas.add(object);//数据存储到list,供批量处理,或后续自己业务逻辑处理。
        doSomething(object);//根据自己业务做处理
    }
    private void doSomething(Object object) {
        //1、入库调用接口
    }
    public void doAfterAllAnalysed(AnalysisContext context) {
        // datas.clear();//解析结束销毁不用的资源
    }
    public List<Object> getDatas() {
        return datas;
    }
    public void setDatas(List<Object> datas) {
        this.datas = datas;
    }

}

读取:
public static void saxReadSheetsV2007() throws IOException {
        InputStream inputStream = new FileInputStream(new File("F:/Excel/demo02.xlsx"));
        BufferedInputStream inputStreams = new BufferedInputStream(inputStream);

       ExcelListener excelListener = new ExcelListener();

        ExcelReader excelReader = EasyExcelFactory.getReader(inputStreams,excelListener);

        List<Sheet> sheets = excelReader.getSheets();
        System.out.println();
        for (Sheet sheet:sheets) {
            if(sheet.getSheetNo() ==1) {
                excelReader.read(sheet);
            }else if(sheet.getSheetNo() ==2){
                excelReader.read(sheet);
            }else if(sheet.getSheetNo() ==3){
                excelReader.read(sheet);
            }
           System.out.println(sheet.getSheetNo());
        }
        inputStream.close();
    }

xlsx-streamer

**github地址:https://github.com/monitorjbl/excel-streaming-reader**
使用xlsx-streamer可以解决OMM的问题,它是一种流式的读取方式。
引入maven依赖:
		       <dependency>
		            <groupId>org.apache.commons</groupId>
		            <artifactId>commons-compress</artifactId>
		            <version>1.18</version>
		        </dependency>

        <dependency>
            <groupId>com.monitorjbl</groupId>
            <artifactId>xlsx-streamer</artifactId>
            <version>2.1.0</version>
        </dependency>

简单使用

  public static void main(String[] args) {

        try (
                InputStream is = new FileInputStream(new File("F:/Excel/sss.xlsx"));
                Workbook workbook = StreamingReader.builder()
                        .rowCacheSize(100)
                        .bufferSize(4096)
                        .open(is)) {
            for (Sheet sheet : workbook){
                System.out.println(sheet.getSheetName());
                for (Row r : sheet) {
                    for (Cell c : r) {
                        System.out.println(c.getStringCellValue());
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

问题

当使用xlsx-streamer读取excel的时候,如果文件达到500M左右,会抛出大概如下异常:
 "Zip bomb detected! The file would exceed the max size of the expanded data in the zip-file.This may indicates that the file is used to inflate memory usage and thus could pose a security risk.You can adjust this limit via ZipSecureFile.setMaxEntrySize() if you need to work with files which are very large.


原因是poi进行了限制  [0-4GB]:
     /**
     * Sets the maximum file size of a single zip entry. It defaults to 4GB,
     * i.e. the 32-bit zip format maximum.
     * 
     * This can be used to limit memory consumption and protect against 
     * security vulnerabilities when documents are provided by users.
     *
     * @param maxEntrySize the max. file size of a single zip entry
     */
    public static void setMaxEntrySize(long maxEntrySize) {
        if (maxEntrySize < 0 || maxEntrySize > 0xFFFFFFFFL) {   // don't use MAX_ENTRY_SIZE here!
            throw new IllegalArgumentException("Max entry size is bounded [0-4GB], but had " + maxEntrySize);
        }
        MAX_ENTRY_SIZE = maxEntrySize;
    }

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消