Issues instantiating NPOIFSFileSystem Object with File

…衆ロ難τιáo~ 提交于 2019-12-12 03:34:41

问题


First up I am asking this question from home =- away from all the source; I will attempt to update tomorrow.

Essentially I am instantiating NPOIFSFileSystem like so:

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

The getLocalFile() method returns a String to a file path - I know the file exists in that location. However, the instatiation is returning an error:

ArrayIndexOutOfBounds

Does anyone have any ideas on this at all?

The file in question is an XLS file, it does contain Arabic characters and I wonder if that could cause the problem.

I did previously have this using POIFSFileSystem but the XLS file in question caused some issues and various forums recommended moving to NPOIFSFileSystem

So, has anyone seen this before? Or can anyone offer me some tips/pointers on this please?

If any further clarity is needed just ask and tomorrow, when I'm back at work (and we have fixed our internet connection) I will update this question.

Thanks in advance Nathan

EDIT 1 Here is the full stack trace:

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)

I am running POI3-8

I will upgrade to 3.9 and try again and let you know.

Thanks again Nathan

EDIT 2

I have updated to 3.9 and will test again soon - just waiting for some other items to complete prior to testing. I will post back the results.

Thanks Nathan

EDIT 3 Same odd problem. Here is the stack trace now I'm using 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)

Thanks Nathan

EDIT 4 I have switched from NPOIFSFileSystem to POIFSFileSystem:

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

This give me the following stack trace:

    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)

I know the file exists and is a valid XLS file.

I am using Java 1.7 and Poi 3.9.

The application is running from /usr/java/data_map_tool and the files are downloaded to and processed from /home/javaapp/data_map_files

The application runs under the javaapp user who is the owner of the directories in question.

At one point I had the files downloaded to a directory within the directory the application is running under and that worked - however, the file size prohibits on going use of that as a the file store.

Does anyone have any ideas on this as I am banging my head against the wall with this.


回答1:


Well, I've cracked it! As suggested the issue was with the original source file. To cut a long story short it was BOM. There was a Byte Order Marker at the start of the XSL file. When my Java application opens this with a FileInputStream it causes chaos.

So, I just updated my code to open the file with

BOMInputStream();

from Apache commons io.

This has resolved the issue.

Many thanks Nathan



来源:https://stackoverflow.com/questions/13807167/issues-instantiating-npoifsfilesystem-object-with-file

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