From 662485fb2be7f70b0c3591638fd6d2e399235c4e Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 10 Nov 2021 22:48:11 +0800 Subject: [PATCH] fix FileResourceBug --- CHANGELOG.md | 3 +- .../hutool/core/io/resource/FileResource.java | 32 +++++++++++-------- .../java/cn/hutool/core/date/ZodiacTest.java | 4 +-- .../excel/sax/handler/AbstractRowHandler.java | 4 +-- .../poi/excel/sax/handler/BeanRowHandler.java | 6 ++-- .../poi/excel/sax/handler/MapRowHandler.java | 6 ++-- .../poi/excel/sax/handler/RowHandler.java | 4 +-- .../cn/hutool/poi/excel/ExcelSaxReadTest.java | 4 +-- 8 files changed, 35 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7c09f083..6f9e9ef1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,11 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.17 (2021-11-07) +# 5.7.17 (2021-11-10) ### 🐣新特性 ### 🐞Bug修复 +* 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/io/resource/FileResource.java b/hutool-core/src/main/java/cn/hutool/core/io/resource/FileResource.java index 3c44d8d50..9bac9521c 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/resource/FileResource.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/resource/FileResource.java @@ -1,6 +1,8 @@ package cn.hutool.core.io.resource; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.URLUtil; import java.io.File; @@ -18,11 +20,21 @@ public class FileResource implements Resource, Serializable { private static final long serialVersionUID = 1L; private final File file; + private final String name; // ----------------------------------------------------------------------- Constructor start /** * 构造 * + * @param path 文件绝对路径或相对ClassPath路径,但是这个路径不能指向一个jar包中的文件 + */ + public FileResource(String path) { + this(FileUtil.file(path)); + } + + /** + * 构造,文件名使用文件本身的名字,带扩展名 + * * @param path 文件 * @since 4.4.1 */ @@ -31,37 +43,31 @@ public class FileResource implements Resource, Serializable { } /** - * 构造 + * 构造,文件名使用文件本身的名字,带扩展名 * * @param file 文件 */ public FileResource(File file) { - this(file, file.getName()); + this(file, null); } /** * 构造 * * @param file 文件 - * @param fileName 文件名,如果为null获取文件本身的文件名 + * @param fileName 文件名,带扩展名,如果为null获取文件本身的文件名 */ public FileResource(File file, String fileName) { + Assert.notNull(file, "File must be not null !"); this.file = file; + this.name = ObjectUtil.defaultIfNull(fileName, file.getName()); } - /** - * 构造 - * - * @param path 文件绝对路径或相对ClassPath路径,但是这个路径不能指向一个jar包中的文件 - */ - public FileResource(String path) { - this(FileUtil.file(path)); - } // ----------------------------------------------------------------------- Constructor end @Override public String getName() { - return this.file.getName(); + return this.name; } @Override @@ -89,6 +95,6 @@ public class FileResource implements Resource, Serializable { */ @Override public String toString() { - return (null == this.file) ? "null" : this.file.toString(); + return this.file.toString(); } } diff --git a/hutool-core/src/test/java/cn/hutool/core/date/ZodiacTest.java b/hutool-core/src/test/java/cn/hutool/core/date/ZodiacTest.java index 8d4eacdd7..dac1329c3 100644 --- a/hutool-core/src/test/java/cn/hutool/core/date/ZodiacTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/date/ZodiacTest.java @@ -4,14 +4,14 @@ import org.junit.Assert; import org.junit.Test; public class ZodiacTest { - + @Test public void getZodiacTest() { Assert.assertEquals("摩羯座", Zodiac.getZodiac(Month.JANUARY, 19)); Assert.assertEquals("水瓶座", Zodiac.getZodiac(Month.JANUARY, 20)); Assert.assertEquals("巨蟹座", Zodiac.getZodiac(6, 17)); } - + @Test public void getChineseZodiacTest() { Assert.assertEquals("狗", Zodiac.getChineseZodiac(1994)); diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/AbstractRowHandler.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/AbstractRowHandler.java index 95b71164e..71d31d1c8 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/AbstractRowHandler.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/AbstractRowHandler.java @@ -40,12 +40,12 @@ public abstract class AbstractRowHandler implements RowHandler { } @Override - public void handle(int sheetIndex, long rowIndex, List rowList) { + public void handle(int sheetIndex, long rowIndex, List rowCells) { Assert.notNull(convertFunc); if (rowIndex < this.startRowIndex || rowIndex > this.endRowIndex) { return; } - handleData(sheetIndex, rowIndex, convertFunc.callWithRuntimeException(rowList)); + handleData(sheetIndex, rowIndex, convertFunc.callWithRuntimeException(rowCells)); } /** diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/BeanRowHandler.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/BeanRowHandler.java index 8ff603bcb..60d888ebc 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/BeanRowHandler.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/BeanRowHandler.java @@ -42,11 +42,11 @@ public abstract class BeanRowHandler extends AbstractRowHandler { } @Override - public void handle(int sheetIndex, long rowIndex, List rowList) { + public void handle(int sheetIndex, long rowIndex, List rowCells) { if (rowIndex == this.headerRowIndex) { - this.headerList = ListUtil.unmodifiable(Convert.toList(String.class, rowList)); + this.headerList = ListUtil.unmodifiable(Convert.toList(String.class, rowCells)); return; } - super.handle(sheetIndex, rowIndex, rowList); + super.handle(sheetIndex, rowIndex, rowCells); } } diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/MapRowHandler.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/MapRowHandler.java index 32e3e1c52..a4c53dc0f 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/MapRowHandler.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/MapRowHandler.java @@ -39,11 +39,11 @@ public abstract class MapRowHandler extends AbstractRowHandler rowList) { + public void handle(int sheetIndex, long rowIndex, List rowCells) { if (rowIndex == this.headerRowIndex) { - this.headerList = ListUtil.unmodifiable(Convert.toList(String.class, rowList)); + this.headerList = ListUtil.unmodifiable(Convert.toList(String.class, rowCells)); return; } - super.handle(sheetIndex, rowIndex, rowList); + super.handle(sheetIndex, rowIndex, rowCells); } } diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/RowHandler.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/RowHandler.java index 34de9aa0a..0a85499bb 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/RowHandler.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/sax/handler/RowHandler.java @@ -17,9 +17,9 @@ public interface RowHandler { * * @param sheetIndex 当前Sheet序号 * @param rowIndex 当前行号,从0开始计数 - * @param rowList 行数据列表 + * @param rowCells 行数据,每个Object表示一个单元格的值 */ - void handle(int sheetIndex, long rowIndex, List rowList); + void handle(int sheetIndex, long rowIndex, List rowCells); /** * 处理一个单元格的数据 diff --git a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelSaxReadTest.java b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelSaxReadTest.java index cda21e304..a4f9d13c3 100644 --- a/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelSaxReadTest.java +++ b/hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelSaxReadTest.java @@ -125,7 +125,7 @@ public class ExcelSaxReadTest { } @Override - public void handle(int sheetIndex, long rowIndex, List rowList) { + public void handle(int sheetIndex, long rowIndex, List rowCells) { } } @@ -143,7 +143,7 @@ public class ExcelSaxReadTest { } @Override - public void handle(int sheetIndex, long rowIndex, List rowList) { + public void handle(int sheetIndex, long rowIndex, List rowCells) { } } );