From 59cad1998925b0abd5b17048bc1b7a680a9a1658 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 17 Dec 2020 00:28:18 +0800 Subject: [PATCH] fix bug and add methods --- CHANGELOG.md | 6 +++- .../cn/hutool/core/codec/Base64Encoder.java | 8 ++--- .../main/java/cn/hutool/core/io/FileUtil.java | 2 +- .../java/cn/hutool/core/io/file/PathUtil.java | 24 ++++++++++++++ .../java/cn/hutool/core/util/URLUtil.java | 31 ++++++++++++------- 5 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08bc0260c..d89819707 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,14 @@ ------------------------------------------------------------------------------------------------------------- -# 5.5.5 (2020-12-16) +# 5.5.5 (2020-12-17) ### 新特性 +* 【core 】 URLUtil.normalize新增重载(pr#233@Gitee) +* 【core 】 PathUtil增加isSub和toAbsNormal方法 + ### Bug修复 +* 【core 】 FileUtil.isSub相对路径判断问题(pr#1315@Github) ------------------------------------------------------------------------------------------------------------- # 5.5.4 (2020-12-16) diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/Base64Encoder.java b/hutool-core/src/main/java/cn/hutool/core/codec/Base64Encoder.java index 33c2b700b..1df7cefd8 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/Base64Encoder.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/Base64Encoder.java @@ -1,10 +1,10 @@ package cn.hutool.core.codec; -import java.nio.charset.Charset; - import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.StrUtil; +import java.nio.charset.Charset; + /** * Base64编码 * @@ -128,11 +128,11 @@ public class Base64Encoder { /** * 编码为Base64
- * 如果isMultiLine为true,则每76个字符一个换行符,否则在一行显示 + * 如果isMultiLine为{@code true},则每76个字符一个换行符,否则在一行显示 * * @param arr 被编码的数组 * @param isMultiLine 在76个char之后是CRLF还是EOF - * @param isUrlSafe 是否使用URL安全字符,一般为false + * @param isUrlSafe 是否使用URL安全字符,一般为{@code false} * @return 编码后的bytes */ public static byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) { diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java index 7b06ca8be..d0b59ab37 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -3194,7 +3194,7 @@ public class FileUtil extends PathUtil { public static boolean isSub(File parent, File sub) { Assert.notNull(parent); Assert.notNull(sub); - return sub.toPath().toAbsolutePath().normalize().startsWith(parent.toPath().toAbsolutePath().normalize()); + return isSub(parent.toPath(), sub.toPath()); } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java index cd737c9be..27dd00888 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java @@ -515,4 +515,28 @@ public class PathUtil { final LinkOption[] options = isFollowLinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS}; return Files.exists(path, options); } + + /** + * 判断给定的目录是否为给定文件或文件夹的子目录 + * + * @param parent 父目录 + * @param sub 子目录 + * @return 子目录是否为父目录的子目录 + * @since 5.5.5 + */ + public static boolean isSub(Path parent, Path sub) { + return toAbsNormal(sub).startsWith(toAbsNormal(parent)); + } + + /** + * 将Path路径转换为标准的绝对路径 + * + * @param path 文件或目录Path + * @return 转换后的Path + * @since 5.5.5 + */ + public static Path toAbsNormal(Path path){ + Assert.notNull(path); + return path.toAbsolutePath().normalize(); + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java index 2633b90b4..0026d5353 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java @@ -662,9 +662,11 @@ public class URLUtil { /** * 标准化URL字符串,包括: * - *
-	 * 1. 多个/替换为一个
-	 * 
+ *
    + *
  1. 自动补齐“http://”头
  2. + *
  3. 去除开头的\或者/
  4. + *
  5. 替换\为/
  6. + *
* * @param url URL字符串 * @return 标准化后的URL字符串 @@ -676,9 +678,11 @@ public class URLUtil { /** * 标准化URL字符串,包括: * - *
-	 * 1. 多个/替换为一个
-	 * 
+ *
    + *
  1. 自动补齐“http://”头
  2. + *
  3. 去除开头的\或者/
  4. + *
  5. 替换\为/
  6. + *
* * @param url URL字符串 * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分) @@ -692,15 +696,18 @@ public class URLUtil { /** * 标准化URL字符串,包括: * - *
-	 * 1. 多个/替换为一个
-	 * 
+ *
    + *
  1. 自动补齐“http://”头
  2. + *
  3. 去除开头的\或者/
  4. + *
  5. 替换\为/
  6. + *
  7. 如果replaceSlash为true,则替换多个/为一个
  8. + *
* * @param url URL字符串 * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分) * @param replaceSlash 是否替换url body中的 // * @return 标准化后的URL字符串 - * @since 4.4.1 + * @since 5.5.5 */ public static String normalize(String url, boolean isEncodePath, boolean replaceSlash) { if (StrUtil.isBlank(url)) { @@ -728,10 +735,10 @@ public class URLUtil { // 去除开头的\或者/ //noinspection ConstantConditions body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY); - // 替换多个\或/为单个/ + // 替换\为/ body = body.replace("\\", "/"); - //issue#I25MZL,双斜杠在URL中是允许存在的,默认不做替换 if (replaceSlash) { + //issue#I25MZL@Gitee,双斜杠在URL中是允许存在的,默认不做替换 body = body.replaceAll("//+", "/"); } }