add: 递归下载FTP服务器上文件到本地(支持普通ftp和sftp)

This commit is contained in:
chenzz 2020-05-11 11:09:01 +08:00
parent 8289e6a8da
commit a2303b11db
4 changed files with 117 additions and 26 deletions

View File

@ -154,6 +154,14 @@ public abstract class AbstractFtp implements Closeable {
*/
public abstract void download(String path, File outFile);
/**
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步), 服务器上有新文件会覆盖本地文件
*
* @param sourcePath ftp服务器目录
* @param destinationPath 本地目录
*/
public abstract void recursiveDownloadFolder(String sourcePath, String destinationPath) throws Exception;
// ---------------------------------------------------------------------------------------------------------------------------------------- Private method start
/**
* 是否包含指定字符串忽略大小写

View File

@ -476,6 +476,35 @@ public class Ftp extends AbstractFtp {
download(dir, fileName, outFile);
}
/**
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
*
* @param sourcePath ftp服务器目录
* @param destinationPath 本地目录
*/
@Override
public void recursiveDownloadFolder(String sourcePath, String destinationPath) {
String pathSeparator = "/";
FTPFile[] lsFiles = lsFiles(sourcePath);
for (FTPFile ftpFile : lsFiles) {
String sourcePathPathFile = sourcePath + pathSeparator + ftpFile.getName();
String destinationPathFile = destinationPath + pathSeparator + ftpFile.getName();
if (!ftpFile.isDirectory()) {
// 本地不存在文件或者ftp上文件有修改则下载
if (!FileUtil.exist(destinationPathFile)
|| (ftpFile.getTimestamp().getTimeInMillis() > FileUtil.lastModifiedTime(destinationPathFile).getTime())) {
// Download file from source (source filename, destination filename).
download(sourcePathPathFile, FileUtil.file(destinationPathFile));
}
} else if (!(".".equals(ftpFile.getName()) || "..".equals(ftpFile.getName()))) {
FileUtil.mkdir(destinationPathFile);
recursiveDownloadFolder(sourcePathPathFile, destinationPathFile);
}
}
}
/**
* 下载文件
*

View File

@ -418,6 +418,38 @@ public class Sftp extends AbstractFtp {
get(src, FileUtil.getAbsolutePath(destFile));
}
/**
* 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
*
* @param sourcePath ftp服务器目录
* @param destinationPath 本地目录
*/
@Override
public void recursiveDownloadFolder(String sourcePath, String destinationPath) throws Exception {
String pathSeparator = "/";
Vector<ChannelSftp.LsEntry> fileAndFolderList = channel.ls(sourcePath);
//Iterate through list of folder content
for (ChannelSftp.LsEntry item : fileAndFolderList) {
String sourcePathPathFile = sourcePath + pathSeparator + item.getFilename();
String destinationPathFile = destinationPath + pathSeparator + item.getFilename();
if (!item.getAttrs().isDir()) {
// 本地不存在文件或者ftp上文件有修改则下载
if (!FileUtil.exist(destinationPathFile)
|| (item.getAttrs().getMTime() > (FileUtil.lastModifiedTime(destinationPathFile).getTime() / 1000))) {
// Download file from source (source filename, destination filename).
channel.get(sourcePathPathFile, destinationPathFile);
}
} else if (!(".".equals(item.getFilename()) || "..".equals(item.getFilename()))) {
FileUtil.mkdir(destinationPathFile);
recursiveDownloadFolder(sourcePathPathFile, destinationPathFile);
}
}
}
/**
* 获取远程文件
*

View File

@ -2,6 +2,7 @@ package cn.hutool.extra.ftp;
import java.util.List;
import cn.hutool.extra.ssh.Sftp;
import org.junit.Ignore;
import org.junit.Test;
@ -59,4 +60,25 @@ public class FtpTest {
IoUtil.close(ftp);
}
@Test
@Ignore
public void recursiveDownloadFolder() throws Exception {
Ftp ftp = new Ftp("looly.centos");
ftp.recursiveDownloadFolder("/","d:/test/download");
IoUtil.close(ftp);
}
@Test
@Ignore
public void recursiveDownloadFolderSftp() throws Exception {
Sftp ftp = new Sftp("127.0.0.1", 22, "test", "test");
ftp.cd("/file/aaa");
Console.log(ftp.pwd());
ftp.recursiveDownloadFolder("/","d:/test/download");
IoUtil.close(ftp);
}
}