每日一得

          不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速開發(fā)
          最近關(guān)心的內(nèi)容:SSH,seam,flex,敏捷,TDD
          本站的官方站點(diǎn)是:顛覆軟件

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            220 隨筆 :: 9 文章 :: 421 評(píng)論 :: 0 Trackbacks
          key words : POI java讀取Excel? java.io.IOException Unable to read entire block

          版本:2.5.1final
          錯(cuò)誤提示:
          java.io.IOException Unable to read entire block

          出這個(gè)問題具有隨機(jī)性,有時(shí)候沒問題,有時(shí)候?qū)xcel里的CellType改一下好像就沒問題,但也不總是這樣,真是莫名其妙.

          Google了一下是一個(gè)bug,重新下載src文件,將RawDataBlock.java文件的RawDataBlock(final InputStream stream)constructor覆蓋:

          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");
          ????????}
          ????}


          打包:
          ant?jar

          重啟app,OK!

          說明:
          主要問題出在 InputStream的read上,原來的實(shí)現(xiàn)用ReadFully方法:
          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不能確保返回的是最大字節(jié)數(shù),但是另一個(gè)實(shí)現(xiàn)卻可以:
          ByteInputStream
          所以,下面的方法也可以修改這個(gè)問題:
          ?//?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);

          posted on 2006-08-10 17:09 Alex 閱讀(3596) 評(píng)論(1)  編輯  收藏 所屬分類: java

          評(píng)論

          # re: POI的一個(gè)bug問題 2006-10-30 09:09 yye
          我照著你的方法修改了以后,還是出現(xiàn)同樣的錯(cuò)誤,而且,修改poi源碼那個(gè),使用新的jar包后,連原來可以導(dǎo)入成功的文件也出現(xiàn)了同樣的錯(cuò)誤。。。。能幫忙解決一下嗎?  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 中卫市| 沂水县| 蛟河市| 东方市| 麻栗坡县| 连州市| 区。| 修武县| 克东县| 靖江市| 潼关县| 温泉县| 离岛区| 长治县| 鄯善县| 双柏县| 蒲江县| 梅州市| 文成县| 呼玛县| 都安| 三原县| 台前县| 山西省| 保靖县| 中阳县| 察隅县| 简阳市| 四子王旗| 时尚| 浦江县| 襄樊市| 固原市| 通化县| 高唐县| 高要市| 贵州省| 普兰县| 澄江县| 耿马| 沾益县|