简体   繁体   中英

Unable to read entire header; 0 bytes read; expected 512 bytes

I am using Apache poi 3.8 for reading xls file, but i got exception:

        java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes
        at org.apache.poi.poifs.storage.HeaderBlock.alertShortRead(HeaderBlock.java:226)
        at org.apache.poi.poifs.storage.HeaderBlock.readFirst512(HeaderBlock.java:207)
        at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
        at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)

used code sample:

     FileInputStream myInput = new FileInputStream(excelFilePathWithExtension);
     logger.debug("FileInputStream::"+myInput);

     POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
     logger.debug("POIFSFileSystem::"+myFileSystem);

     Workbook workbook = WorkbookFactory.create(myFileSystem);

please help me?

If we take a look at the HeaderBlocks class, we can see these blocks :

public HeaderBlock(InputStream stream) throws IOException {
    // Grab the first 512 bytes
    // (For 4096 sized blocks, the remaining 3584 bytes are zero)
    // Then, process the contents
    this(readFirst512(stream));
    ...
}

The constructor you used will read the first 512 bytes of your inputstream then call a private constructor.

And the readFirst512 method throw an exception if there is not enough bytes to read.

Also, the POI's document say that a POI file system structure starts starts with a header block of 512 bytes.

So... It seems that your file is not big enough for POI.

Open in MS-Excel and save as different name. Try again.

if you place the files being read or written in different folder you may not get the error. Some default folders like 'Downloads' of Mozilla have restricted permissions, it seems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM