Browse Source

fix readBySax stream bug

Looly 5 years ago
parent
commit
8d1ab0173a

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@
 ### Bug修复
 * 【cron   】     修复CronTimer可能死循环的问题(issue#1224@Github)
 * 【core   】     修复Calculator.conversion单个数字越界问题(issue#1222@Github)
+* 【poi    】     修复ExcelUtil.getSaxReader使用非MarkSupport流报错问题(issue#1225@Github)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 3 - 0
hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelUtil.java

@@ -2,6 +2,7 @@ package cn.hutool.poi.excel;
 
 import cn.hutool.core.exceptions.DependencyException;
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.ReUtil;
 import cn.hutool.core.util.StrUtil;
@@ -84,6 +85,7 @@ public class ExcelUtil {
 	 * @since 3.2.0
 	 */
 	public static void readBySax(InputStream in, int rid, RowHandler rowHandler) {
+		in = IoUtil.toMarkSupportStream(in);
 		final ExcelSaxReader<?> reader = ExcelSaxUtil.createSaxReader(ExcelFileUtil.isXlsx(in), rowHandler);
 		reader.read(in, rid);
 	}
@@ -97,6 +99,7 @@ public class ExcelUtil {
 	 * @since 5.4.4
 	 */
 	public static void readBySax(InputStream in, String idOrRid, RowHandler rowHandler) {
+		in = IoUtil.toMarkSupportStream(in);
 		final ExcelSaxReader<?> reader = ExcelSaxUtil.createSaxReader(ExcelFileUtil.isXlsx(in), rowHandler);
 		reader.read(in, idOrRid);
 	}

+ 7 - 0
hutool-poi/src/test/java/cn/hutool/poi/excel/test/ExcelSaxReadTest.java

@@ -3,6 +3,7 @@ package cn.hutool.poi.excel.test;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.convert.Convert;
 import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.lang.Console;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.poi.excel.ExcelUtil;
@@ -32,6 +33,12 @@ public class ExcelSaxReadTest {
 	}
 
 	@Test
+	public void excel07FromStreamTest() {
+		// issue#1225 非markSupport的流读取会错误
+		ExcelUtil.readBySax(IoUtil.toStream(FileUtil.file("aaa.xlsx")), 0, createRowHandler());
+	}
+
+	@Test
 	public void excel03Test() {
 		Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
 		reader.read("aaa.xls", 1);