mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复PathMover.moveContent问题
This commit is contained in:
parent
2e81de3ecb
commit
afc4e32790
@ -1275,23 +1275,38 @@ public class FileUtil extends PathUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查两个文件是否是同一个文件<br>
|
* 检查两个文件是否是同一个文件<br>
|
||||||
* 所谓文件相同,是指File对象是否指向同一个文件或文件夹
|
* 所谓文件相同,是指File对象是否指向同一个文件或文件夹,规则为:
|
||||||
|
* <ul>
|
||||||
|
* <li>当两个文件都为{@code null}时,返回{@code true}</li>
|
||||||
|
* <li>当两个文件都存在时,检查是否为同一个文件</li>
|
||||||
|
* <li>当两个文件都不存在时,检查路径是否一致</li>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* @param file1 文件1
|
* @param file1 文件1,可以为{@code null}
|
||||||
* @param file2 文件2
|
* @param file2 文件2,可以为{@code null}
|
||||||
* @return 是否相同
|
* @return 是否相同
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public static boolean equals(final File file1, final File file2) throws IORuntimeException {
|
public static boolean equals(final File file1, final File file2) throws IORuntimeException {
|
||||||
Assert.notNull(file1);
|
// 都为null认定为相同
|
||||||
Assert.notNull(file2);
|
if (null == file1 || null == file2) {
|
||||||
if (!file1.exists() || !file2.exists()) {
|
return null == file1 && null == file2;
|
||||||
// 两个文件都不存在判断其路径是否相同, 对于一个存在一个不存在的情况,一定不相同
|
|
||||||
return !file1.exists()//
|
|
||||||
&& !file2.exists()//
|
|
||||||
&& pathEquals(file1, file2);
|
|
||||||
}
|
}
|
||||||
return equals(file1.toPath(), file2.toPath());
|
|
||||||
|
final boolean exists1 = file1.exists();
|
||||||
|
final boolean exists2 = file2.exists();
|
||||||
|
|
||||||
|
// 当两个文件都存在时,检查是否为同一个文件
|
||||||
|
if (exists1 && exists2) {
|
||||||
|
return PathUtil.isSameFile(file1.toPath(), file2.toPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 都不存在时,检查路径是否相同
|
||||||
|
if (!exists1 && !exists2) {
|
||||||
|
return pathEquals(file1, file2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,7 +96,7 @@ public class PathMover {
|
|||||||
final CopyOption[] options = this.options;
|
final CopyOption[] options = this.options;
|
||||||
|
|
||||||
if (PathUtil.isSub(src, target)) {
|
if (PathUtil.isSub(src, target)) {
|
||||||
if(Files.exists(target) && PathUtil.equals(src, target)){
|
if(PathUtil.equals(src, target)){
|
||||||
// issue#2845,当用户传入目标路径与源路径一致时,直接返回,否则会导致删除风险。
|
// issue#2845,当用户传入目标路径与源路径一致时,直接返回,否则会导致删除风险。
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
@ -144,15 +144,11 @@ public class PathMover {
|
|||||||
*/
|
*/
|
||||||
public Path moveContent() {
|
public Path moveContent() {
|
||||||
final Path src = this.src;
|
final Path src = this.src;
|
||||||
if (PathUtil.isExistsAndNotDirectory(target, false)) {
|
|
||||||
// 文件移动调用move方法
|
|
||||||
return move();
|
|
||||||
}
|
|
||||||
|
|
||||||
final Path target = this.target;
|
final Path target = this.target;
|
||||||
|
|
||||||
|
// 文件移动调用move方法
|
||||||
if (PathUtil.isExistsAndNotDirectory(target, false)) {
|
if (PathUtil.isExistsAndNotDirectory(target, false)) {
|
||||||
// 目标不能为文件
|
return move();
|
||||||
throw new IllegalArgumentException("Can not move dir content to a file");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// issue#2893 target 不存在导致NoSuchFileException
|
// issue#2893 target 不存在导致NoSuchFileException
|
||||||
|
@ -19,6 +19,7 @@ import org.dromara.hutool.core.io.resource.FileResource;
|
|||||||
import org.dromara.hutool.core.io.resource.Resource;
|
import org.dromara.hutool.core.io.resource.Resource;
|
||||||
import org.dromara.hutool.core.lang.Assert;
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -611,6 +612,11 @@ public class PathUtil {
|
|||||||
/**
|
/**
|
||||||
* 检查两个文件是否是同一个文件<br>
|
* 检查两个文件是否是同一个文件<br>
|
||||||
* 所谓文件相同,是指Path对象是否指向同一个文件或文件夹
|
* 所谓文件相同,是指Path对象是否指向同一个文件或文件夹
|
||||||
|
* <ul>
|
||||||
|
* <li>当两个文件都为{@code null}时,返回{@code true}</li>
|
||||||
|
* <li>当两个文件都存在时,检查是否为同一个文件</li>
|
||||||
|
* <li>当两个文件都不存在时,检查Path对象是否equals</li>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* @param file1 文件1
|
* @param file1 文件1
|
||||||
* @param file2 文件2
|
* @param file2 文件2
|
||||||
@ -620,6 +626,34 @@ public class PathUtil {
|
|||||||
* @since 5.4.1
|
* @since 5.4.1
|
||||||
*/
|
*/
|
||||||
public static boolean equals(final Path file1, final Path file2) throws IORuntimeException {
|
public static boolean equals(final Path file1, final Path file2) throws IORuntimeException {
|
||||||
|
// 都为null认定为相同
|
||||||
|
if(null == file1 || null == file2){
|
||||||
|
return null == file1 && null == file2;
|
||||||
|
}
|
||||||
|
|
||||||
|
final boolean exists1 = exists(file1, false);
|
||||||
|
final boolean exists2 = exists(file2, false);
|
||||||
|
|
||||||
|
|
||||||
|
if(exists1 && exists2){
|
||||||
|
return isSameFile(file1, file2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ObjUtil.equals(file1, file2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查两个文件是否是同一个文件<br>
|
||||||
|
* 所谓文件相同,是指Path对象是否指向同一个文件或文件夹
|
||||||
|
*
|
||||||
|
* @param file1 文件1,必须存在
|
||||||
|
* @param file2 文件2,必须存在
|
||||||
|
* @return 是否相同
|
||||||
|
* @throws IORuntimeException IO异常
|
||||||
|
* @see Files#isSameFile(Path, Path)
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public static boolean isSameFile(final Path file1, final Path file2) throws IORuntimeException {
|
||||||
try {
|
try {
|
||||||
return Files.isSameFile(file1, file2);
|
return Files.isSameFile(file1, file2);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user