Browse Source

fix bug and add methods

Looly 5 years ago
parent
commit
59cad19989

+ 5 - 1
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)

+ 4 - 4
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<br>
-	 * 如果isMultiLine为<code>true</code>,则每76个字符一个换行符,否则在一行显示
+	 * 如果isMultiLine为{@code true},则每76个字符一个换行符,否则在一行显示
 	 * 
 	 * @param arr 被编码的数组
 	 * @param isMultiLine 在76个char之后是CRLF还是EOF
-	 * @param isUrlSafe 是否使用URL安全字符,一般为<code>false</code>
+	 * @param isUrlSafe 是否使用URL安全字符,一般为{@code false}
 	 * @return 编码后的bytes
 	 */
 	public static byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) {

+ 1 - 1
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());
 	}
 
 	/**

+ 24 - 0
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();
+	}
 }

+ 19 - 12
hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java

@@ -662,9 +662,11 @@ public class URLUtil {
 	/**
 	 * 标准化URL字符串,包括:
 	 *
-	 * <pre>
-	 * 1. 多个/替换为一个
-	 * </pre>
+	 * <ol>
+	 *     <li>自动补齐“http://”头</li>
+	 *     <li>去除开头的\或者/</li>
+	 *     <li>替换\为/</li>
+	 * </ol>
 	 *
 	 * @param url URL字符串
 	 * @return 标准化后的URL字符串
@@ -676,9 +678,11 @@ public class URLUtil {
 	/**
 	 * 标准化URL字符串,包括:
 	 *
-	 * <pre>
-	 * 1. 多个/替换为一个
-	 * </pre>
+	 * <ol>
+	 *     <li>自动补齐“http://”头</li>
+	 *     <li>去除开头的\或者/</li>
+	 *     <li>替换\为/</li>
+	 * </ol>
 	 *
 	 * @param url          URL字符串
 	 * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分)
@@ -692,15 +696,18 @@ public class URLUtil {
 	/**
 	 * 标准化URL字符串,包括:
 	 *
-	 * <pre>
-	 * 1. 多个/替换为一个
-	 * </pre>
+	 * <ol>
+	 *     <li>自动补齐“http://”头</li>
+	 *     <li>去除开头的\或者/</li>
+	 *     <li>替换\为/</li>
+	 *     <li>如果replaceSlash为true,则替换多个/为一个</li>
+	 * </ol>
 	 *
 	 * @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("//+", "/");
 			}
 		}