[英]JVM crashing while writing to XLSX file( POI)
尝试写入.xlsx文件时JVM崩溃。 我正在使用POI(XSSF)。 代码中的错误位置点是write method--> workBook.write(fileOutputStream);
在控制台我得到..
A fatal error has been detected by the Java Runtime Environment:
SIGBUS (0x7) at pc=0xb68d77f3, pid=14653, tid=1849355120
JRE version: 7.0_04-b20
Java VM: Java HotSpot(TM) Server VM (23.0-b21 mixed mode linux-x86 )
Problematic frame:
C [libzip.so+0x47f3] newEntry+0x73
Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
If you would like to submit a bug report, please visit:
http://bugreport.sun.com/bugreport/crash.jsp
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.
我找到的解决方案,我一直在寻找一段时间,是为了确保你不打开你用来打开FileOutputStream
以保存Workbook
Workbook
的File
的Workbook
。 而是使用FileInputStream
打开Workbook
。
像这样的东西将完美无缺
File inputFile = new File("Your-Path");
this.inputStream = new FileInputStream(inputFile);
this.opc = OPCPackage.open(this.inputStream);
this.workbook = WorkbookFactory.create(opc);
...
this.outputStream = new FileOutputStream(inputFile);
this.workbook.write(this.outputStream);
不要忘记关闭每个打开的流和OPCPackage
。
其他解决方案都不适合我。 我只需要只读访问我的Excel文件,并设置readonly标志为我工作:
Workbook wb = new XSSFWorkbook(OPCPackage.open(file, PackageAccess.READ));
Workbook wb = new HSSFWorkbook(new POIFSFileSystem(file, true));
使用OPCPackage
并没有为我修复JVM崩溃,但使用了WorkbookFactory
。 如果您查看POI Busy Developers Guide,它们提供了读取和写入相同Excel文件的示例。
File excelFile = new File("workbook.xlsx");
InputStream inp = new FileInputStream(excelFile);
Workbook wb = WorkbookFactory.create(inp);
FileOutputStream fileOut = new FileOutputStream(excelFile);
wb.write(fileOut);
fileOut.close();
使用Apache POI 3.13版,Java 1.8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.