Apache POI throwing OOM error when trying to password protect modest sized XLSX workbooks (~=80MB)

穿精又带淫゛_ 提交于 2021-01-29 16:14:21

问题


using java 8 and apache POI and POI-OOXML 3.14

Here's my method:

    private static void encryptXlsx(String inputPath, String outputPath, String password) throws IOException, InvalidFormatException, GeneralSecurityException {
    //create a new workbook
    Workbook wb = WorkbookFactory.create(new File(inputPath));


    //Add password protection and encrypt the file
    POIFSFileSystem fs = new POIFSFileSystem();
    EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
    Encryptor enc = info.getEncryptor();
    enc.confirmPassword(password); // s3cr3t is your password to open sheet.

    OPCPackage opc = OPCPackage.open(new File(inputPath), PackageAccess.READ_WRITE);
    OutputStream os = enc.getDataStream(fs);
    opc.save(os);
    opc.close();

    FileOutputStream fos = new FileOutputStream(outputPath);
    fs.writeFilesystem(fos);
    fos.close();
}

Error stack is:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.apache.xmlbeans.impl.store.Cur$CurLoadContext.attr(Cur.java:3044)
at org.apache.xmlbeans.impl.store.Cur$CurLoadContext.attr(Cur.java:3065)
at org.apache.xmlbeans.impl.store.Locale$SaxHandler.startElement(Locale.java:3263)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportStartTag(Piccolo.java:1082)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseAttributesNS(PiccoloLexer.java:1802)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseOpenTagNS(PiccoloLexer.java:1521)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseTagNS(PiccoloLexer.java:1362)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseXMLNS(PiccoloLexer.java:1293)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseXML(PiccoloLexer.java:1261)
at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.yylex(PiccoloLexer.java:4812)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yylex(Piccolo.java:1290)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yyparse(Piccolo.java:1400)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:714)
at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3479)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1277)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1264)
at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
at org.apache.poi.POIXMLTypeLoader.parse(POIXMLTypeLoader.java:92)
at org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument$Factory.parse(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:173)
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:165)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.parseSheet(XSSFWorkbook.java:417)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:382)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:178)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:249)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)
at XlsxEncryptExample.encryptXlsx(XlsxEncryptExample.java:36)
at XlsxEncryptExample.main(XlsxEncryptExample.java:28)

Process finished with exit code 1

If anybody could point me to a way to read this file in without using this much memory, a lib that does password protecting better than POI or any other possible sol'n I'd be grateful.


回答1:


As I understand the documentation POI normally reads the entire spreadsheet into memory. It does say

Note that the usermodel system has a higher memory footprint than the low level eventusermodel, but has the major advantage of being much simpler to work with. Also please be aware that as the new XSSF supported Excel 2007 OOXML (.xlsx) files are XML based, the memory footprint for processing them is higher than for the older HSSF supported (.xls) binary files.

It continues to offer low-memory footprint alternatives.




回答2:


private static void encryptXlsx(String inputPath, String outputPath, String password) throws IOException, InvalidFormatException, GeneralSecurityException {
//create a new workbook
//Workbook wb = WorkbookFactory.create(new File(inputPath));


//Add password protection and encrypt the file
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword(password); // s3cr3t is your password to open sheet.

OPCPackage opc = OPCPackage.open(new File(inputPath), PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();

FileOutputStream fos = new FileOutputStream(outputPath);
fs.writeFilesystem(fos);
fos.close();

}

Eliminating the call to workbook factory seems to have worked for me. It turns out you don't need to load the whole file to password protect it. Still doesn't solve the issue for anyone who wants to manipulate the contents of a file, however, so I'm leaving the question unsolved.



来源:https://stackoverflow.com/questions/61463301/apache-poi-throwing-oom-error-when-trying-to-password-protect-modest-sized-xlsx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!