diff --git a/CHANGELOG.md b/CHANGELOG.md
index ad50d9095..b3ac555e8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@
* 【json 】 JSONUtil.toJsonStr增加重载,支持JSONConfig(issue#I48H5L@Gitee)
* 【crypto 】 SymmetricCrypto增加setMode方法,update采用累加模式(pr#1642@Github)
* 【core 】 ZipReader支持Filter
+* 【all 】 Sftp、Ftp、HttpDownloader增加download重载,支持避免传输文件损坏(pr#407@Gitee)
### 🐞Bug修复
* 【core 】 修复ListUtil.split方法越界问题(issue#I48Q0P@Gitee)
diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java
index 9b9f7f55f..56d5f4c26 100644
--- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java
@@ -1056,7 +1056,7 @@ public class FileUtil extends PathUtil {
*
*
* @param file 被修改的文件
- * @param newName 新的文件名,包括扩展名
+ * @param newName 新的文件名,可选是否包括扩展名
* @param isRetainExt 是否保留原文件的扩展名,如果保留,则newName不需要加扩展名
* @param isOverride 是否覆盖目标文件
* @return 目标文件
diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java
index f29a7face..052e79de6 100644
--- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java
+++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/AbstractFtp.java
@@ -178,6 +178,41 @@ public abstract class AbstractFtp implements Closeable {
*/
public abstract void download(String path, File outFile);
+ /**
+ * 下载文件-避免未完成的文件
+ * 来自:https://gitee.com/dromara/hutool/pulls/407
+ * 此方法原理是先在目标文件同级目录下创建临时文件,下载之,等下载完毕后重命名,避免因下载错误导致的文件不完整。
+ *
+ * @param path 文件路径
+ * @param outFile 输出文件或目录
+ * @param tempFileSuffix 临时文件后缀,默认".temp"
+ * @since 5.7.12
+ */
+ public void download(String path, File outFile, String tempFileSuffix) {
+ if(StrUtil.isBlank(tempFileSuffix)){
+ tempFileSuffix = ".temp";
+ } else {
+ tempFileSuffix = StrUtil.addPrefixIfNot(tempFileSuffix, StrUtil.DOT);
+ }
+
+ // 目标文件真实名称
+ final String fileName = outFile.isDirectory() ? FileUtil.getName(path) : outFile.getName();
+ // 临时文件名称
+ final String tempFileName = fileName + tempFileSuffix;
+
+ // 临时文件
+ outFile = new File(outFile.isDirectory() ? outFile : outFile.getParentFile(), tempFileName);
+ try {
+ download(path, outFile);
+ // 重命名下载好的临时文件
+ FileUtil.rename(outFile, fileName, true);
+ } catch (Throwable e) {
+ // 异常则删除临时文件
+ FileUtil.del(outFile);
+ throw new FtpException(e);
+ }
+ }
+
/**
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步), 服务器上有新文件会覆盖本地文件
*
diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java
index 2b2f77880..a74af7073 100644
--- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java
+++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java
@@ -25,7 +25,7 @@ import java.util.List;
/**
* FTP客户端封装
* 此客户端基于Apache-Commons-Net
- *
+ *
* 常见搭建ftp的工具有
* 1、filezila server ;根目录一般都是空
* 2、linux vsftpd ; 使用的 系统用户的目录,这里往往都是不是根目录,如:/home/ftpuser/ftp
@@ -556,8 +556,8 @@ public class Ftp extends AbstractFtp {
/**
* 下载文件
*
- * @param path 文件路径
- * @param outFile 输出文件或目录
+ * @param path 文件路径,包含文件名
+ * @param outFile 输出文件或目录,当为目录时,使用服务端的文件名
*/
@Override
public void download(String path, File outFile) {
@@ -599,9 +599,9 @@ public class Ftp extends AbstractFtp {
/**
* 下载文件
*
- * @param path 文件路径
+ * @param path 文件所在路径(远程目录),不包含文件名
* @param fileName 文件名
- * @param outFile 输出文件或目录
+ * @param outFile 输出文件或目录,当为目录时使用服务端文件名
* @throws IORuntimeException IO异常
*/
public void download(String path, String fileName, File outFile) throws IORuntimeException {
@@ -632,10 +632,10 @@ public class Ftp extends AbstractFtp {
/**
* 下载文件到输出流
*
- * @param path 文件路径
- * @param fileName 文件名
- * @param out 输出位置
- * @param fileNameCharset 文件名编码
+ * @param path 服务端的文件路径
+ * @param fileName 服务端的文件名
+ * @param out 输出流,下载的文件写出到这个流中
+ * @param fileNameCharset 文件名编码,通过此编码转换文件名编码为ISO8859-1
* @throws IORuntimeException IO异常
* @since 5.5.7
*/
diff --git a/hutool-extra/src/test/java/cn/hutool/extra/qrcode/QrCodeUtilTest.java b/hutool-extra/src/test/java/cn/hutool/extra/qrcode/QrCodeUtilTest.java
index 331416ac2..cfb5dd251 100644
--- a/hutool-extra/src/test/java/cn/hutool/extra/qrcode/QrCodeUtilTest.java
+++ b/hutool-extra/src/test/java/cn/hutool/extra/qrcode/QrCodeUtilTest.java
@@ -84,7 +84,7 @@ public class QrCodeUtilTest {
@Test
@Ignore
public void decodeTest3(){
- final String decode = QrCodeUtil.decode(ImgUtil.read("d:/test/qr_a.png"), true, true);
+ final String decode = QrCodeUtil.decode(ImgUtil.read("d:/test/qr_a.png"), false, true);
Console.log(decode);
}
}
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpDownloader.java b/hutool-http/src/main/java/cn/hutool/http/HttpDownloader.java
index 51aca79bb..1a8ca528b 100644
--- a/hutool-http/src/main/java/cn/hutool/http/HttpDownloader.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpDownloader.java
@@ -11,8 +11,8 @@ import java.nio.charset.Charset;
/**
* 下载封装,下载统一使用{@code GET}请求,默认支持30x跳转
*
- * @since 5.6.4
* @author looly
+ * @since 5.6.4
*/
public class HttpDownloader {
@@ -43,27 +43,43 @@ public class HttpDownloader {
/**
* 下载远程文件
*
- * @param url 请求的url
- * @param destFile 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
- * @param timeout 超时,单位毫秒,-1表示默认超时
- * @param streamProgress 进度条
+ * @param url 请求的url
+ * @param targetFileOrDir 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
+ * @param timeout 超时,单位毫秒,-1表示默认超时
+ * @param streamProgress 进度条
* @return 文件大小
*/
- public static long downloadFile(String url, File destFile, int timeout, StreamProgress streamProgress) {
- return requestDownload(url, timeout).writeBody(destFile, streamProgress);
+ public static long downloadFile(String url, File targetFileOrDir, int timeout, StreamProgress streamProgress) {
+ return requestDownload(url, timeout).writeBody(targetFileOrDir, streamProgress);
+ }
+
+ /**
+ * 下载文件-避免未完成的文件
+ * 来自:https://gitee.com/dromara/hutool/pulls/407
+ * 此方法原理是先在目标文件同级目录下创建临时文件,下载之,等下载完毕后重命名,避免因下载错误导致的文件不完整。
+ *
+ * @param url 请求的url
+ * @param targetFileOrDir 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
+ * @param tempFileSuffix 临时文件后缀,默认".temp"
+ * @param timeout 超时,单位毫秒,-1表示默认超时
+ * @param streamProgress 进度条
+ * @since 5.7.12
+ */
+ public long downloadFile(String url, File targetFileOrDir, String tempFileSuffix, int timeout, StreamProgress streamProgress) {
+ return requestDownload(url, timeout).writeBody(targetFileOrDir, tempFileSuffix, streamProgress);
}
/**
* 下载远程文件,返回文件
*
- * @param url 请求的url
- * @param destFile 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
- * @param timeout 超时,单位毫秒,-1表示默认超时
- * @param streamProgress 进度条
+ * @param url 请求的url
+ * @param targetFileOrDir 目标文件或目录,当为目录时,取URL中的文件名,取不到使用编码后的URL做为文件名
+ * @param timeout 超时,单位毫秒,-1表示默认超时
+ * @param streamProgress 进度条
* @return 文件
*/
- public static File downloadForFile(String url, File destFile, int timeout, StreamProgress streamProgress) {
- return requestDownload(url, timeout).writeBodyForFile(destFile, streamProgress);
+ public static File downloadForFile(String url, File targetFileOrDir, int timeout, StreamProgress streamProgress) {
+ return requestDownload(url, timeout).writeBodyForFile(targetFileOrDir, streamProgress);
}
/**
@@ -84,8 +100,8 @@ public class HttpDownloader {
/**
* 请求下载文件
*
- * @param url 请求下载文件地址
- * @param timeout 超时时间
+ * @param url 请求下载文件地址
+ * @param timeout 超时时间
* @return HttpResponse
* @since 5.4.1
*/
diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java
index b8823aeef..eda670ac5 100644
--- a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java
+++ b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java
@@ -271,9 +271,7 @@ public class HttpResponse extends HttpBase
* 写出后会关闭Http流(异步模式)
*
- * @param destFile 写出到的文件
- * @param streamProgress 进度显示接口,通过实现此接口显示下载进度
+ * @param targetFileOrDir 写出到的文件或目录
+ * @param streamProgress 进度显示接口,通过实现此接口显示下载进度
* @return 写出bytes数
* @since 3.3.2
*/
- public long writeBody(File destFile, StreamProgress streamProgress) {
- Assert.notNull(destFile, "[destFile] is null!");
+ public long writeBody(File targetFileOrDir, StreamProgress streamProgress) {
+ Assert.notNull(targetFileOrDir, "[targetFileOrDir] must be not null!");
- final File outFile = completeFileNameFromHeader(destFile);
+ final File outFile = completeFileNameFromHeader(targetFileOrDir);
return writeBody(FileUtil.getOutputStream(outFile), true, streamProgress);
}
+ /**
+ * 将响应内容写出到文件-避免未完成的文件
+ * 异步模式下直接读取Http流写出,同步模式下将存储在内存中的响应内容写出
+ * 写出后会关闭Http流(异步模式)
+ * 来自:https://gitee.com/dromara/hutool/pulls/407
+ * 此方法原理是先在目标文件同级目录下创建临时文件,下载之,等下载完毕后重命名,避免因下载错误导致的文件不完整。
+ *
+ * @param targetFileOrDir 写出到的文件或目录
+ * @param tempFileSuffix 临时文件后缀,默认".temp"
+ * @param streamProgress 进度显示接口,通过实现此接口显示下载进度
+ * @return 写出bytes数
+ * @since 5.7.12
+ */
+ public long writeBody(File targetFileOrDir, String tempFileSuffix, StreamProgress streamProgress) {
+ Assert.notNull(targetFileOrDir, "[targetFileOrDir] must be not null!");
+
+ File outFile = completeFileNameFromHeader(targetFileOrDir);
+
+ if (StrUtil.isBlank(tempFileSuffix)) {
+ tempFileSuffix = ".temp";
+ } else {
+ tempFileSuffix = StrUtil.addPrefixIfNot(tempFileSuffix, StrUtil.DOT);
+ }
+
+ // 目标文件真实名称
+ final String fileName = outFile.getName();
+ // 临时文件名称
+ final String tempFileName = fileName + tempFileSuffix;
+
+ // 临时文件
+ outFile = new File(outFile.getParentFile(), tempFileName);
+
+ long length;
+ try {
+ length = writeBody(outFile, streamProgress);
+ // 重命名下载好的临时文件
+ FileUtil.rename(outFile, fileName, true);
+ } catch (Throwable e) {
+ // 异常则删除临时文件
+ FileUtil.del(outFile);
+ throw new HttpException(e);
+ }
+ return length;
+ }
+
/**
* 将响应内容写出到文件
* 异步模式下直接读取Http流写出,同步模式下将存储在内存中的响应内容写出
* 写出后会关闭Http流(异步模式)
*
- * @param destFile 写出到的文件
- * @param streamProgress 进度显示接口,通过实现此接口显示下载进度
+ * @param targetFileOrDir 写出到的文件
+ * @param streamProgress 进度显示接口,通过实现此接口显示下载进度
* @return 写出的文件
* @since 5.6.4
*/
- public File writeBodyForFile(File destFile, StreamProgress streamProgress) {
- Assert.notNull(destFile, "[destFile] is null!");
+ public File writeBodyForFile(File targetFileOrDir, StreamProgress streamProgress) {
+ Assert.notNull(targetFileOrDir, "[targetFileOrDir] must be not null!");
- final File outFile = completeFileNameFromHeader(destFile);
+ final File outFile = completeFileNameFromHeader(targetFileOrDir);
writeBody(FileUtil.getOutputStream(outFile), true, streamProgress);
return outFile;
@@ -326,12 +369,12 @@ public class HttpResponse extends HttpBase
* 写出后会关闭Http流(异步模式)
*
- * @param destFile 写出到的文件
+ * @param targetFileOrDir 写出到的文件或目录
* @return 写出bytes数
* @since 3.3.2
*/
- public long writeBody(File destFile) {
- return writeBody(destFile, null);
+ public long writeBody(File targetFileOrDir) {
+ return writeBody(targetFileOrDir, null);
}
/**
@@ -339,12 +382,12 @@ public class HttpResponse extends HttpBase
* 写出后会关闭Http流(异步模式)
*
- * @param destFilePath 写出到的文件的路径
+ * @param targetFileOrDir 写出到的文件或目录的路径
* @return 写出bytes数
* @since 3.3.2
*/
- public long writeBody(String destFilePath) {
- return writeBody(FileUtil.file(destFilePath));
+ public long writeBody(String targetFileOrDir) {
+ return writeBody(FileUtil.file(targetFileOrDir));
}
// ---------------------------------------------------------------- Body end
@@ -373,14 +416,14 @@ public class HttpResponse extends HttpBase