简体   繁体   English

无法读取整个标题; 0字节读取; 预计512字节

[英]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: 我使用Apache poi 3.8来读取xls文件,但我得到了异常:

        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 : 如果我们看一下HeaderBlocks类,我们可以看到这些块:

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. 您使用的构造函数将读取输入流的前512个字节,然后调用私有构造函数。

And the readFirst512 method throw an exception if there is not enough bytes to read. 如果没有足够的字节可读, readFirst512方法会抛出异常。

Also, the POI's document say that a POI file system structure starts starts with a header block of 512 bytes. 此外, POI的文档说POI文件系统结构从一个512字节的标题块开始。

So... It seems that your file is not big enough for POI. 所以......看起来你的文件对于POI来说还不够大。

Open in MS-Excel and save as different name. 在MS-Excel中打开并另存为不同的名称。 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. 一些默认文件夹,如Mozilla的“下载”,似乎有限制权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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