我正在尝试通过将.xlsx作为资源流传递来使用Apache-POI创建xlsx工作簿对象:
Workbook workbook = WorkbookFactory.create(RFQGenerator.class.getResourceAsStream("/com/lumos/xlsx_manager/es/da/template.xlsx"));
我可以通过FileInputStream和本地文件路径传递template.xlsx时成功实例化工作簿,但是当我传递资源流时,我得到一个例外:
java.io.IOException: Failed to read zip entry source
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:103)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:184)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:149)
at com.lumos.xlsx_manager.es.da.RFQGenerator.createRfq(RFQGenerator.java:30)
at com.lumos.xlsx_manager.es.da.Firebase$2.onChildAdded(Firebase.java:88)
at com.google.firebase.database.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:81)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:65)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.zip.ZipException: invalid code lengths set
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:194)
at org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream.read(ZipSecureFile.java:220)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:132)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:56)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:100)
... 11 more
我传递的.xlsx不应该被压缩,但也许这就是它如何与资源流一起工作。关于如何通过资源访问将文件传递到Apache-POI的任何想法?
答案 0 :(得分:2)
当使用maven资源时,你必须对二进制文件进行不过滤。
Maven corrupting binary files in source/main/resources when building jar