Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileUtil.copyFile()实际效果与注释不一致 #3557

Closed
LeonemZhang opened this issue Apr 24, 2024 · 1 comment
Closed

FileUtil.copyFile()实际效果与注释不一致 #3557

LeonemZhang opened this issue Apr 24, 2024 · 1 comment
Labels

Comments

@LeonemZhang
Copy link

版本情况

JDK版本: oraclejdk_8_202
hutool版本: 5.8.27(请确保最新尝试是否还有问题)

问题描述(包括截图)

方法注释说目的文件不存在会创建,但是实际运行的时候会报NoSuchFileException,显然是没有创建出目的文件

  1. 复现代码
            URL url = new URL(fileUrl);
            URLConnection connection = url.openConnection();
            String fileName = url.getFile().substring(url.getFile().lastIndexOf('/') + 1);
            final File downloadFile = FileUtil.file(destFolderPath, fileName);
            try (InputStream inputStream = connection.getInputStream()) {
                log.info("开始下载文件:{}", fileUrl);
                FileUtil.copyFile(inputStream, downloadFile, StandardCopyOption.REPLACE_EXISTING);
                log.info("文件已下载到:{}", destFolderPath);
            }
  1. 堆栈信息
cn.hutool.core.io.IORuntimeException: NoSuchFileException: C:\test\Snipaste-2.8.8-Beta-x64.zip
	at cn.hutool.core.io.file.PathUtil.copyFile(PathUtil.java:223)
	at cn.hutool.core.io.FileUtil.copyFile(FileUtil.java:1072)
	at com.zzsn.download.FileDownloader.downloadFile(FileDownloader.java:26)
	at com.zzsn.download.FileDownloader.main(FileDownloader.java:41)
Caused by: java.nio.file.NoSuchFileException: C:\test\Snipaste-2.8.8-Beta-x64.zip
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
	at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
	at java.nio.file.Files.newOutputStream(Files.java:216)
	at java.nio.file.Files.copy(Files.java:3016)
	at cn.hutool.core.io.file.PathUtil.copyFile(PathUtil.java:221)
	... 3 common frames omitted
cn.hutool.core.io.IORuntimeException: NoSuchFileException: C:\test\Snipaste-2.8.8-Beta-x64.zip
	at cn.hutool.core.io.file.PathUtil.copyFile(PathUtil.java:223)
	at cn.hutool.core.io.FileUtil.copyFile(FileUtil.java:1072)
	at com.zzsn.download.FileDownloader.downloadFile(FileDownloader.java:26)
	at com.zzsn.download.FileDownloader.main(FileDownloader.java:41)
Caused by: java.nio.file.NoSuchFileException: C:\test\Snipaste-2.8.8-Beta-x64.zip
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
	at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
	at java.nio.file.Files.newOutputStream(Files.java:216)
	at java.nio.file.Files.copy(Files.java:3016)
	at cn.hutool.core.io.file.PathUtil.copyFile(PathUtil.java:221)
	... 3 more
@looly looly added the bug label Apr 24, 2024
@looly
Copy link
Member

looly commented Apr 24, 2024

确实存在这个问题。

问题在于:

  1. 如果只是文件不存在,则不会报错
  2. 如果文件所在目录不存在,则会报错。

5.8.28 修复此问题!

@looly looly closed this as completed Apr 24, 2024
@looly looly changed the title FileUtiil.copyFile()实际效果与注释不一致 FileUtil.copyFile()实际效果与注释不一致 Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants