java 无法读取整个标题;读取 0 个字节;预期 512 字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12616689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to read entire header; 0 bytes read; expected 512 bytes
提问by Sameek Mishra
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?
请帮我?
回答by DayS
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 documentsay 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。
回答by kfatih
Open in MS-Excel and save as different name. Try again.
在 MS-Excel 中打开并另存为不同的名称。再试一次。
回答by ravi
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 的“下载”)具有受限权限。