fix bug and add methods

This commit is contained in:
Looly 2020-12-17 00:28:18 +08:00
parent 89300c5f4d
commit 59cad19989
5 changed files with 53 additions and 18 deletions

View File

@ -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修复 ### Bug修复
* 【core 】 FileUtil.isSub相对路径判断问题pr#1315@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.5.4 (2020-12-16) # 5.5.4 (2020-12-16)

View File

@ -1,10 +1,10 @@
package cn.hutool.core.codec; package cn.hutool.core.codec;
import java.nio.charset.Charset;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import java.nio.charset.Charset;
/** /**
* Base64编码 * Base64编码
* *
@ -128,11 +128,11 @@ public class Base64Encoder {
/** /**
* 编码为Base64<br> * 编码为Base64<br>
* 如果isMultiLine为<code>true</code>则每76个字符一个换行符否则在一行显示 * 如果isMultiLine为{@code true}则每76个字符一个换行符否则在一行显示
* *
* @param arr 被编码的数组 * @param arr 被编码的数组
* @param isMultiLine 在76个char之后是CRLF还是EOF * @param isMultiLine 在76个char之后是CRLF还是EOF
* @param isUrlSafe 是否使用URL安全字符一般为<code>false</code> * @param isUrlSafe 是否使用URL安全字符一般为{@code false}
* @return 编码后的bytes * @return 编码后的bytes
*/ */
public static byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) { public static byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) {

View File

@ -3194,7 +3194,7 @@ public class FileUtil extends PathUtil {
public static boolean isSub(File parent, File sub) { public static boolean isSub(File parent, File sub) {
Assert.notNull(parent); Assert.notNull(parent);
Assert.notNull(sub); Assert.notNull(sub);
return sub.toPath().toAbsolutePath().normalize().startsWith(parent.toPath().toAbsolutePath().normalize()); return isSub(parent.toPath(), sub.toPath());
} }
/** /**

View File

@ -515,4 +515,28 @@ public class PathUtil {
final LinkOption[] options = isFollowLinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS}; final LinkOption[] options = isFollowLinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS};
return Files.exists(path, options); 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();
}
} }

View File

@ -662,9 +662,11 @@ public class URLUtil {
/** /**
* 标准化URL字符串包括 * 标准化URL字符串包括
* *
* <pre> * <ol>
* 1. 多个/替换为一个 * <li>自动补齐http://</li>
* </pre> * <li>去除开头的\或者/</li>
* <li>替换\/</li>
* </ol>
* *
* @param url URL字符串 * @param url URL字符串
* @return 标准化后的URL字符串 * @return 标准化后的URL字符串
@ -676,9 +678,11 @@ public class URLUtil {
/** /**
* 标准化URL字符串包括 * 标准化URL字符串包括
* *
* <pre> * <ol>
* 1. 多个/替换为一个 * <li>自动补齐http://</li>
* </pre> * <li>去除开头的\或者/</li>
* <li>替换\/</li>
* </ol>
* *
* @param url URL字符串 * @param url URL字符串
* @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义不包括 http:, /和域名部分 * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义不包括 http:, /和域名部分
@ -692,15 +696,18 @@ public class URLUtil {
/** /**
* 标准化URL字符串包括 * 标准化URL字符串包括
* *
* <pre> * <ol>
* 1. 多个/替换为一个 * <li>自动补齐http://</li>
* </pre> * <li>去除开头的\或者/</li>
* <li>替换\/</li>
* <li>如果replaceSlash为true则替换多个/为一个</li>
* </ol>
* *
* @param url URL字符串 * @param url URL字符串
* @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义不包括 http:, /和域名部分 * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义不包括 http:, /和域名部分
* @param replaceSlash 是否替换url body中的 // * @param replaceSlash 是否替换url body中的 //
* @return 标准化后的URL字符串 * @return 标准化后的URL字符串
* @since 4.4.1 * @since 5.5.5
*/ */
public static String normalize(String url, boolean isEncodePath, boolean replaceSlash) { public static String normalize(String url, boolean isEncodePath, boolean replaceSlash) {
if (StrUtil.isBlank(url)) { if (StrUtil.isBlank(url)) {
@ -728,10 +735,10 @@ public class URLUtil {
// 去除开头的\或者/ // 去除开头的\或者/
//noinspection ConstantConditions //noinspection ConstantConditions
body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY); body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY);
// 替换多个\/单个/ // 替换\/
body = body.replace("\\", "/"); body = body.replace("\\", "/");
//issue#I25MZL双斜杠在URL中是允许存在的默认不做替换
if (replaceSlash) { if (replaceSlash) {
//issue#I25MZL@Gitee双斜杠在URL中是允许存在的默认不做替换
body = body.replaceAll("//+", "/"); body = body.replaceAll("//+", "/");
} }
} }