FileUtil.copy,当来源为文件时,返回文件而非目录

This commit is contained in:
Looly 2022-10-24 22:28:35 +08:00
parent 2142d6522a
commit 2bafc6c8a8
3 changed files with 18 additions and 6 deletions

View File

@ -7,6 +7,7 @@
### 🐣新特性
* 【http 】 HttpResponse增加getFileNameFromDisposition方法pr#2676@Github
* 【core 】 FileUtil.copy当来源为文件时返回文件而非目录
### 🐞Bug修复
* 【db 】 修复分页时order by截断问题issue#I5X6FM@Gitee

View File

@ -168,7 +168,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
@Override
public File copy() throws IORuntimeException{
final File src = this.src;
final File dest = this.dest;
File dest = this.dest;
// check
Assert.notNull(src, "Source File is null !");
if (false == src.exists()) {
@ -191,7 +191,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
final File subTarget = isCopyContentIfDir ? dest : FileUtil.mkdir(FileUtil.file(dest, src.getName()));
internalCopyDirContent(src, subTarget);
} else {// 复制文件
internalCopyFile(src, dest);
dest = internalCopyFile(src, dest);
}
return dest;
}
@ -244,14 +244,15 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
* 2如果目标是一个已存在的目录则文件拷贝到此目录下文件名与原文件名一致
* </pre>
*
* @param src 源文件必须为文件
* @param src 源文件必须为文件
* @param dest 目标文件如果非覆盖模式必须为目录
* @return 目标的目录或文件
* @throws IORuntimeException IO异常
*/
private void internalCopyFile(File src, File dest) throws IORuntimeException {
private File internalCopyFile(File src, File dest) throws IORuntimeException {
if (null != copyFilter && false == copyFilter.accept(src)) {
//被过滤的文件跳过
return;
return src;
}
// 如果已经存在目标文件切为不覆盖模式跳过之
@ -263,7 +264,7 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
if(dest.exists() && false == isOverride) {
//非覆盖模式跳过
return;
return src;
}
}else {
//路径不存在则创建父目录
@ -283,6 +284,8 @@ public class FileCopier extends SrcToDestCopier<File, FileCopier>{
} catch (IOException e) {
throw new IORuntimeException(e);
}
return dest;
}
//----------------------------------------------------------------------------------------- Private method end
}

View File

@ -502,4 +502,12 @@ public class FileUtilTest {
path = "test\\aaa.txt";
Assert.assertFalse(FileUtil.isAbsolutePath(path));
}
@Test
@Ignore
public void copyTest2(){
final File copy = FileUtil.copy("d:/test/qrcodeCustom.png", "d:/test/pic", false);
// 当复制文件到目标目录的时候返回复制的目标文件而非目录
Console.log(copy);
}
}