使用File实例化NPOIFSFileSystem对象的问题

时间:2012-12-10 18:44:05

标签: java apache-poi

首先我在家里问这个问题= - 远离所有来源;我明天会尝试更新。

基本上我是这样实例化NPOIFSFileSystem:

NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(this.getLocalFile()));

getLocalFile()方法将String返回到文件路径 - 我知道该文件存在于该位置。但是,instatiation返回错误:

ArrayIndexOutOfBounds

有人对此有任何想法吗?

有问题的文件是一个XLS文件,它确实包含阿拉伯字符,我想知道这是否会导致问题。

我以前使用过POIFSFileSystem但是有问题的XLS文件引起了一些问题,各种论坛建议转移到NPOIFSFileSystem

那么,有没有人见过这个?或者,有人可以给我一些提示/指示吗?

如果需要进一步明确,只需要询问明天,当我重返工作岗位时(我们已修复了我们的互联网连接),我将更新此问题。

提前致谢 森

编辑1 这是完整的堆栈跟踪:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at org.apache.poi.poifs.filesystem.BlockStore$ChainLoopDetector.claim(BlockStore.java:95)
    at org.apache.poi.poifs.filesystem.NPOIFSStream$StreamBlockByteBufferIterator.next(NPOIFSStream.java:212)
    at org.apache.poi.poifs.filesystem.NPOIFSStream$StreamBlockByteBufferIterator.next(NPOIFSStream.java:186)
    at org.apache.poi.poifs.property.NPropertyTable.buildProperties(NPropertyTable.java:88)
    at org.apache.poi.poifs.property.NPropertyTable.<init>(NPropertyTable.java:66)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.readCoreContents(NPOIFSFileSystem.java:379)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:202)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:163)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:145)
    at com.turnitin.datamap.parser.standard.XLSParser.defineParsableObject(XLSParser.java:67)
    at com.turnitin.datamap.parser.standard.XLSParser.setUp(XLSParser.java:56)
    at com.turnitin.datamap.parser.standard.XLSParser.<init>(XLSParser.java:46)
    at com.turnitin.datamap.controller.DataMapController.parseData(DataMapController.java:255)
    at com.turnitin.datamap.controller.DataMapController.processControl(DataMapController.java:162)
    at com.turnitin.datamap.controller.DataMapController.processStart(DataMapController.java:130)
    at com.turnitin.datamap.controller.DataMapController.main(DataMapController.java:61)

我正在运行POI3-8

我会升级到3.9,然后再试一次让你知道。

再次感谢 森

编辑2

我已经更新到3.9并将很快再次测试 - 只是在测试前等待其他一些项目完成。我会回复结果。

由于 森

编辑3 同样奇怪的问题。这是堆栈跟踪,现在我正在使用POI 3.9:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at org.apache.poi.poifs.filesystem.BlockStore$ChainLoopDetector.claim(BlockStore.java:95)
    at org.apache.poi.poifs.filesystem.NPOIFSStream$StreamBlockByteBufferIterator.next(NPOIFSStream.java:212)
    at org.apache.poi.poifs.filesystem.NPOIFSStream$StreamBlockByteBufferIterator.next(NPOIFSStream.java:186)
    at org.apache.poi.poifs.property.NPropertyTable.buildProperties(NPropertyTable.java:88)
    at org.apache.poi.poifs.property.NPropertyTable.<init>(NPropertyTable.java:66)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.readCoreContents(NPOIFSFileSystem.java:379)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:202)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:163)
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:145)
    at com.turnitin.datamap.parser.standard.XLSParser.defineParsableObject(XLSParser.java:67)
    at com.turnitin.datamap.parser.standard.XLSParser.setUp(XLSParser.java:56)
    at com.turnitin.datamap.parser.standard.XLSParser.<init>(XLSParser.java:46)
    at com.turnitin.datamap.controller.DataMapController.parseData(DataMapController.java:264)
    at com.turnitin.datamap.controller.DataMapController.processControl(DataMapController.java:162)
    at com.turnitin.datamap.controller.DataMapController.processStart(DataMapController.java:130)
    at com.turnitin.datamap.controller.DataMapController.main(DataMapController.java:61)

由于 森

编辑4 我已经从NPOIFSFileSystem切换到POIFSFileSystem:

FileInputStream fis = new FileInputStream(this.getFileToParse());
    POIFSFileSystem excelFile = new POIFSFileSystem(fis);
    Workbook wb = WorkbookFactory.create(excelFile);

这给了我以下堆栈跟踪:

    java.io.IOException: block[ 1273 ] already removed - does your POIFS have circular or duplicate block references?
    at org.apache.poi.poifs.storage.BlockListImpl.remove(BlockListImpl.java:89)
    at org.apache.poi.poifs.storage.RawDataBlockList.remove(RawDataBlockList.java:34)
    at org.apache.poi.poifs.storage.BlockAllocationTableReader.fetchBlocks(BlockAllocationTableReader.java:221)
    at org.apache.poi.poifs.storage.BlockListImpl.fetchBlocks(BlockListImpl.java:123)
    at org.apache.poi.poifs.storage.RawDataBlockList.fetchBlocks(RawDataBlockList.java:34)
    at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:63)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:159)
    at com.turnitin.datamap.parser.standard.XLSParser.defineParsableObject(XLSParser.java:68)
    at com.turnitin.datamap.parser.standard.XLSParser.setUp(XLSParser.java:56)
    at com.turnitin.datamap.parser.standard.XLSParser.<init>(XLSParser.java:46)
    at com.turnitin.datamap.controller.DataMapController.parseData(DataMapController.java:264)
    at com.turnitin.datamap.controller.DataMapController.processControl(DataMapController.java:162)
    at com.turnitin.datamap.controller.DataMapController.processStart(DataMapController.java:130)
    at com.turnitin.datamap.controller.DataMapController.main(DataMapController.java:61)

我知道该文件存在且是一个有效的XLS文件。

我正在使用Java 1.7和Poi 3.9。

应用程序从/ usr / java / data_map_tool运行,文件从/ home / javaapp / data_map_files下载并处理

应用程序在javaapp用户下运行,该用户是相关目录的所有者。

有一次,我将文件下载到运行应用程序的目录中的一个目录中并且有效 - 但是,文件大小禁止将其用作文件存储。

有没有人对此有任何想法,因为我正用这个撞到墙上。

1 个答案:

答案 0 :(得分:0)

好吧,我已经破解了!正如所建议的那样,问题出在原始源文件中。简而言之,这就是BOM。在XSL文件的开头有一个字节顺序标记。当我的Java应用程序使用FileInputStream打开它时,它会导致混乱。

所以,我刚刚更新了我的代码,用

打开文件
BOMInputStream();

来自Apache commons io。

这解决了这个问题。

非常感谢 森

相关问题