mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 04:37:59 +08:00
!121 递归下载FTP服务器上文件到本地(文件目录和服务器同步)
Merge pull request !121 from chenzz/v5-dev
This commit is contained in:
commit
fb6d9d35fd
@ -154,6 +154,14 @@ public abstract class AbstractFtp implements Closeable {
|
|||||||
*/
|
*/
|
||||||
public abstract void download(String path, File outFile);
|
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
|
// ---------------------------------------------------------------------------------------------------------------------------------------- Private method start
|
||||||
/**
|
/**
|
||||||
* 是否包含指定字符串,忽略大小写
|
* 是否包含指定字符串,忽略大小写
|
||||||
|
@ -476,6 +476,35 @@ public class Ftp extends AbstractFtp {
|
|||||||
download(dir, fileName, outFile);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件
|
||||||
*
|
*
|
||||||
|
@ -418,6 +418,38 @@ public class Sftp extends AbstractFtp {
|
|||||||
get(src, FileUtil.getAbsolutePath(destFile));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取远程文件
|
* 获取远程文件
|
||||||
*
|
*
|
||||||
|
@ -2,6 +2,7 @@ package cn.hutool.extra.ftp;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import cn.hutool.extra.ssh.Sftp;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -59,4 +60,25 @@ public class FtpTest {
|
|||||||
|
|
||||||
IoUtil.close(ftp);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user