From c0807bfa49bada7e8640f0de90eb8d1cb7eb7691 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 24 Apr 2024 13:22:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DFileUtil.copyFile=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=88=9B=E5=BB=BA=E7=88=B6=E7=9B=AE=E5=BD=95=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- .../java/cn/hutool/core/io/file/PathUtil.java | 3 +++ .../cn/hutool/core/io/file/Issue3557Test.java | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 hutool-core/src/test/java/cn/hutool/core/io/file/Issue3557Test.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 53e5a8718..7a5ed7763 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.28(2024-04-22) +# 5.8.28(2024-04-24) ### 🐣新特性 * 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee) @@ -23,6 +23,7 @@ * 【http 】 修复HttpRequest.header相同key被覆盖问题(issue#I9I61C@Gitee) * 【core 】 修复TemporalAccessorConverter自定义格式转换问题(issue#I9HQQE@Gitee) * 【cron 】 修复CronPattern.nextMatchAfter匹配初始值问题(issue#I9FQUA@Gitee) +* 【core 】 修复FileUtil.copyFile没有创建父目录导致的问题(issue#3557@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.27(2024-03-29) 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 e19be8359..c14cfa206 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 @@ -217,6 +217,9 @@ public class PathUtil { Assert.notNull(src, "Source is null !"); Assert.notNull(target, "Destination File or directory is null !"); + // 创建级联父目录 + mkParentDirs(target); + try { Files.copy(src, target, options); } catch (IOException e) { diff --git a/hutool-core/src/test/java/cn/hutool/core/io/file/Issue3557Test.java b/hutool-core/src/test/java/cn/hutool/core/io/file/Issue3557Test.java new file mode 100644 index 000000000..d9f191098 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/io/file/Issue3557Test.java @@ -0,0 +1,18 @@ +package cn.hutool.core.io.file; + +import cn.hutool.core.io.FileUtil; +import org.junit.Ignore; +import org.junit.Test; + +import java.nio.file.StandardCopyOption; + +public class Issue3557Test { + + @Test + @Ignore + public void copyFileTest() { + // 如果只是文件不存在,则不会报错 + // 如果文件所在目录不存在,则会报错 + FileUtil.copyFile(FileUtil.getInputStream("d:/test/aaa.xlsx"), FileUtil.file("d:/test2/aaa_copy.xlsx"), StandardCopyOption.REPLACE_EXISTING); + } +}