Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7587620 建立时间:2006年5月29日 |

| |
[Apache(jakarta)]POI的一个bug问题 软件技术
lhwork 发表于 2006/8/11 9:58:21 |
key words : POI java读取Excel java.io.IOException Unable to read entire block版本:2.5.1final错误提示:java.io.IOException Unable to read entire block出这个问题具有随机性,有时候没问题,有时候将Excel里的CellType改一下好像就没问题,但也不总是这样,真是莫名其妙.Google了一下是一个bug,重新下载src文件,将RawDataBlock.java文件的RawDataBlock(final InputStream stream)constructor覆盖:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public RawDataBlock(final InputStream stream) throws IOException { _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ]; int count = 0; int totalBytesRead = 0; while ((totalBytesRead < POIFSConstants.BIG_BLOCK_SIZE) &&(count != -1)) { count = stream.read(_data, totalBytesRead,POIFSConstants.BIG_BLOCK_SIZE - totalBytesRead); if (count != -1) { totalBytesRead += count; } } if (count == -1) { _eof = true; } else { _eof = false; } if ((totalBytesRead != POIFSConstants.BIG_BLOCK_SIZE) && (totalBytesRead != 0)) { String type = " byte" + ((totalBytesRead == 1) ? (""): ("s")); throw new IOException("Unable to read entire block; " +totalBytesRead + type + " read; expected " + POIFSConstants.BIG_BLOCK_SIZE + "bytes"); } }打包:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->ant jar重启app,OK!说明:主要问题出在 InputStream的read上,原来的实现用ReadFully方法:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public static int readFully(InputStream in, byte[] b, int off, int len) throws IOException { int total = 0; for (;;) { int got = in.read(b, off + total, len - total); if (got < 0) { return (total == 0) ? -1 : total; } else { total += got; if (total == len) return total; } } }InputStream的read不能确保返回的是最大字节数,但是另一个实现却可以:ByteInputStream所以,下面的方法也可以修改这个问题:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> // read entire stream into byte array: ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int count; while (count = inputStream.read(buffer)) != -1) byteOS.append(buffer, 0, count); byteOS.close(); byte[] allBytes = byteOS.betByteArray();// create workbook from array:InputStream byteIS = new ByteArrayInputStream(allBytes);HSSFWorkbook wb = new HSSFWorkbook(byteIS); |
|
回复:POI的一个bug问题 软件技术
kevin(游客)发表评论于2007/1/6 12:51:56 |
|
回复:POI的一个bug问题 软件技术
kevin(游客)发表评论于2006/12/29 13:04:31 |
我试了两种方法都不行哦。
第一种:替换后报的错是一样的。
第二种:编译都过不了。说ByteArrayOutputStream没有append的方法,这一句byte[] allBytes = byteOS.betByteArray();也好象写错了,是不是该改为:byte[] allBytes = byteOS.toByteArray();
谢谢。请看到后帮忙指正。回邮件给我kevin_pjw@21cn.com |
|
|