add method

This commit is contained in:
Looly 2021-09-07 13:33:37 +08:00
parent ac79e77912
commit 758fb793fc
7 changed files with 146 additions and 51 deletions

View File

@ -14,6 +14,7 @@
* 【json 】 JSONUtil.toJsonStr增加重载支持JSONConfigissue#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

View File

@ -1056,7 +1056,7 @@ public class FileUtil extends PathUtil {
* </pre>
*
* @param file 被修改的文件
* @param newName 新的文件名包括扩展名
* @param newName 新的文件名可选是否包括扩展名
* @param isRetainExt 是否保留原文件的扩展名如果保留则newName不需要加扩展名
* @param isOverride 是否覆盖目标文件
* @return 目标文件

View File

@ -178,6 +178,41 @@ public abstract class AbstractFtp implements Closeable {
*/
public abstract void download(String path, File outFile);
/**
* 下载文件-避免未完成的文件<br>
* 来自https://gitee.com/dromara/hutool/pulls/407<br>
* 此方法原理是先在目标文件同级目录下创建临时文件下载之等下载完毕后重命名避免因下载错误导致的文件不完整
*
* @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服务器上文件到本地(文件目录和服务器同步), 服务器上有新文件会覆盖本地文件
*

View File

@ -25,7 +25,7 @@ import java.util.List;
/**
* FTP客户端封装<br>
* 此客户端基于Apache-Commons-Net
*
* <p>
* 常见搭建ftp的工具有
* 1filezila server ;根目录一般都是空
* 2linux 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
*/

View File

@ -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);
}
}

View File

@ -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);
}
/**
* 下载文件-避免未完成的文件<br>
* 来自https://gitee.com/dromara/hutool/pulls/407<br>
* 此方法原理是先在目标文件同级目录下创建临时文件下载之等下载完毕后重命名避免因下载错误导致的文件不完整
*
* @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
*/

View File

@ -271,9 +271,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
* @since 3.3.2
*/
public long writeBody(OutputStream out, boolean isCloseOut, StreamProgress streamProgress) {
if (null == out) {
throw new NullPointerException("[out] is null!");
}
Assert.notNull(out, "[out] must be not null!");
final long contentLength = contentLength();
try {
return copyBody(bodyStream(), out, contentLength, streamProgress);
@ -290,32 +288,77 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
* 异步模式下直接读取Http流写出同步模式下将存储在内存中的响应内容写出<br>
* 写出后会关闭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);
}
/**
* 将响应内容写出到文件-避免未完成的文件<br>
* 异步模式下直接读取Http流写出同步模式下将存储在内存中的响应内容写出<br>
* 写出后会关闭Http流异步模式<br>
* 来自https://gitee.com/dromara/hutool/pulls/407<br>
* 此方法原理是先在目标文件同级目录下创建临时文件下载之等下载完毕后重命名避免因下载错误导致的文件不完整
*
* @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;
}
/**
* 将响应内容写出到文件<br>
* 异步模式下直接读取Http流写出同步模式下将存储在内存中的响应内容写出<br>
* 写出后会关闭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<HttpResponse> implements Closeable {
* 异步模式下直接读取Http流写出同步模式下将存储在内存中的响应内容写出<br>
* 写出后会关闭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<HttpResponse> implements Closeable {
* 异步模式下直接读取Http流写出同步模式下将存储在内存中的响应内容写出<br>
* 写出后会关闭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<HttpResponse> implements Closeable {
/**
* 从响应头补全下载文件名
*
* @param destFile 目标文件夹或者目标文件
* @param targetFileOrDir 目标文件夹或者目标文件
* @return File 保存的文件
* @since 5.4.1
*/
public File completeFileNameFromHeader(File destFile) {
if (false == destFile.isDirectory()) {
public File completeFileNameFromHeader(File targetFileOrDir) {
if (false == targetFileOrDir.isDirectory()) {
// 非目录直接返回
return destFile;
return targetFileOrDir;
}
// 从头信息中获取文件名
@ -394,7 +437,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
fileName = URLUtil.encodeQuery(path, CharsetUtil.CHARSET_UTF_8);
}
}
return FileUtil.file(destFile, fileName);
return FileUtil.file(targetFileOrDir, fileName);
}
// ---------------------------------------------------------------- Private method start
@ -517,7 +560,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
}
final long contentLength = contentLength();
final FastByteArrayOutputStream out = new FastByteArrayOutputStream((int)contentLength);
final FastByteArrayOutputStream out = new FastByteArrayOutputStream((int) contentLength);
copyBody(in, out, contentLength, null);
this.bodyBytes = out.toByteArray();
}