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

@@ -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的工具有
* 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
*/

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