Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7570812 建立时间: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问题 软件技术
薛帅(游客)发表评论于2013/6/21 15:03:06 |
|
回复:POI的一个bug问题 软件技术
rrrrrr(游客)发表评论于2010/8/11 23:04:07 |
金箭营销软件论坛群发 www.qtbiz.cn 论坛群发软件 www.qtbiz.cn 免费发帖软件 www.qtbiz.cn 金箭营销软件论坛群发 www.qtbiz.cn 自动发帖软件 www.qtbiz.cn 发帖器 www.qtbiz.cn 免费版发帖机器人 www.qtbiz.cn 论坛自动发帖软 www.qtbiz.cn 自动发帖 www.qtbiz.cn 免费发帖机 www.qtbiz.cn 论坛自动发帖机 www.qtbiz.cn 分类信息群发 www.qtbiz.cn 黄页群发 www.qtbiz.cn 商务群发 www.qtbiz.cn 博客群发 |
|
回复:POI的一个bug问题 软件技术
河风(游客)发表评论于2008/10/15 9:06:45 |
场景:
我用一个工作薄对象
HSSFWorkbook wb = new HSSFWorkbook();
循环创建了多个不同名称的sheet,同时往sheet内写入数据
for (; i < times; i++) {
sheet = wb.createSheet(name + i);
if (!writeToSheet(sheet, (i * MAX_ROW), ((i + 1) * MAX_ROW), ardInfoList)) {// 向Sheet添加数据
return false;
}
}
最后就是输出这个工作薄流到磁盘文件。
问题描述:
我输出了一个34.2 MB (35,899,904 字节),可是我打开看里面的内容却远没有这么多数据。当我关闭这个excel的时候,文件大小却变为了155 KB (159,232 字节),晕的很?是我哪里弄错了?
请问各位:
如何正确保存和打开多sheet文件呢?
|
|
回复:POI的一个bug问题 软件技术
zhzrat(游客)发表评论于2008/4/17 16:22:11 |
求正确方法解决问题。read entire block问题 |
|
回复:POI的一个bug问题 软件技术
zhzrat(游客)发表评论于2008/4/17 16:12:08 |
我的问题是这样出现的:
我先做了个读写模块,然后再把EXCLE导入MYSQL。发现EXCLE经过读写以后都会报这样的错误。不经过读写的EXCLE读入MYSQL正常。我想问问是不是我这读写模块不正确。
<%@ page contentType="text/html; charset=gbk" %><%@ page import="javax.servlet.ServletInputStream" %><%@ page import="java.util.*" %><%@ page import="java.io.*" %><%@ page import="java.sql.*"%> <%@ page import="org.apache.poi.poifs.filesystem.*,java.io.*,java.text.SimpleDateFormat,org.apache.poi.hssf.usermodel.*,javax.naming.InitialContext"%>
<html><head><title>get method</title><meta http-equiv="Content-Type" content="text/html; charset=GBK"></head><body><% request.setCharacterEncoding("gbk");
String name = null; String value = null; boolean fileFlag = false; String TMP_DIR = "C:/"; File tmpFile = null; String fName = null; FileOutputStream baos = null; BufferedOutputStream bos = null; Hashtable paramHt = new Hashtable();int BUFSIZE = 1024 * 8; int rtnPos = 0; byte[] buffs = new byte[ BUFSIZE * 8 ]; String contentType = request.getContentType(); int index = contentType.indexOf( "boundary=" );String boundary = "--" + contentType.substring( index + 9 ); String endBoundary = boundary + "--"; ServletInputStream sis = request.getInputStream(); while( (rtnPos = sis.readLine( buffs, 0, buffs.length )) != -1 ){ String strBuff = new String( buffs, 0, rtnPos ); if( strBuff.startsWith( boundary ) ){ if ( name != null && name.trim().length() > 0 ){ if (fileFlag ){
bos.flush(); baos.close(); bos.close(); baos = null; bos = null; }else{ Object obj = paramHt.get(name); ArrayList al = null;if ( obj == null ){ al = new ArrayList();}else{ al = (ArrayList)obj; }al.add(value);paramHt.put(name, al);} } name = new String(); value = new String(); fileFlag = false;
rtnPos = sis.readLine( buffs, 0, buffs.length );if (rtnPos != -1 ){strBuff = new String( buffs, 0, rtnPos ); if (strBuff.toLowerCase().startsWith( "content-disposition: form-data; " )){ int nIndex = strBuff.toLowerCase().indexOf( "name=\"" ); int nLastIndex = strBuff.toLowerCase().indexOf( "\"", nIndex + 6 ); name = strBuff.substring( nIndex + 6, nLastIndex );} int fIndex = strBuff.toLowerCase().indexOf( "filename=\"" ); if (fIndex != -1 ){ fileFlag = true; int fLastIndex = strBuff.toLowerCase().indexOf( "\"", fIndex + 10 ); fName = strBuff.substring( fIndex + 10 , fLastIndex ); fIndex = fName.lastIndexOf( "\\" ); if( fIndex == -1 ){ fIndex = fName.lastIndexOf( "/" ); if( fIndex != -1 ){ fName = fName.substring( fIndex + 1 ); } }else{ fName = fName.substring( fIndex + 1 );}if (fName == null || fName.trim().length() == 0){ fileFlag = false; sis.readLine( buffs, 0, buffs.length );sis.readLine( buffs, 0, buffs.length );sis.readLine( buffs, 0, buffs.length );continue;}}sis.readLine( buffs, 0, buffs.length );sis.readLine( buffs, 0, buffs.length ); }
}else if( strBuff.startsWith( endBoundary ) ){ if ( name != null && name.trim().length() > 0 ){ if (fileFlag ){
bos.flush(); baos.close(); bos.close(); baos = null; bos = null; }else{ Object obj = paramHt.get(name); ArrayList al = null;if ( obj == null ){ al = new ArrayList();}else{ al = (ArrayList)obj; }al.add(value);
paramHt.put(name, al);} }
}else{ if (fileFlag ){ if ( baos == null && bos == null ) { tmpFile = new File( TMP_DIR + fName ); baos = new FileOutputStream( tmpFile ); bos = new BufferedOutputStream( baos ); } bos.write( buffs, 0, rtnPos );baos.flush();
}else{ value = value + strBuff;} } }
%>因为excel默认的大小必须是512k的整数倍。所以出现这个问题大多是因为excel文件不规范所致解决办法,把excel打开再关闭。就可以了。 |
|
回复:POI的一个bug问题 软件技术
zhzrat(游客)发表评论于2008/4/17 16:05:55 |
我也做了以下修改,还是报错! stream to byte[]这个方法,我修改了一下File file = new File("e:\\test.xls"); InputStream ism = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes = new byte[(int) file.length()]; //file is object of java.io.File for which you want the byte array int count; while ((count = ism.read(bytes)) != -1) bos.write(bytes, 0, count); byte[] allBytes = bos.toByteArray();可是似乎还是不能解决去我的问题我的没有报错信息,java.io.IOException Unable to read entire block,就是在读和写某些EXCEL文件过程中,写的文件会被破坏。 |
|
回复:POI的一个bug问题 软件技术
真(游客)发表评论于2008/2/25 15:05:00 |
|
回复:POI的一个bug问题 软件技术
alfie(游客)发表评论于2007/8/29 16:41:37 |
stream to byte[]这个方法,我修改了一下File file = new File("e:\\test.xls"); InputStream ism = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bytes = new byte[(int) file.length()]; //file is object of java.io.File for which you want the byte array int count; while ((count = ism.read(bytes)) != -1) bos.write(bytes, 0, count); byte[] allBytes = bos.toByteArray();可是似乎还是不能解决去我的问题我的没有报错信息,java.io.IOException Unable to read entire block,就是在读和写某些EXCEL文件过程中,写的文件会被破坏。 |
|
回复:POI的一个bug问题 软件技术
alfie(游客)发表评论于2007/8/29 13:32:40 |
关注中,同求解决方法//read entire stream into byte array这个方法能写得全一点吗email:alfie820@qq.com |
|
回复:POI的一个bug问题 软件技术
GoYa(游客)发表评论于2007/7/29 15:25:12 |
不知道这是哪里的解决方法,没有数据了,就是再多的循环也没有用。
因为excel默认的大小必须是512k的整数倍。所以出现这个问题大多是因为excel文件不规范所致
解决办法,把excel打开再关闭。就可以了。 |
|
|