【发布时间】:2017-08-22 10:42:30
【问题描述】:
我想从 SFTP 获取文件,这些文件是在 java 中的给定时间戳(最后一次拉取时间)之后创建的。到目前为止,我正在使用 j2ssh。请让我知道是否有其他 API 支持此类功能。
【问题讨论】:
我想从 SFTP 获取文件,这些文件是在 java 中的给定时间戳(最后一次拉取时间)之后创建的。到目前为止,我正在使用 j2ssh。请让我知道是否有其他 API 支持此类功能。
【问题讨论】:
Jsch 支持 ls 命令,它将带您返回远程文件的所有属性。您可以编写一些代码来消除要从那里检索的文件。
Java 文档:http://epaul.github.io/jsch-documentation/javadoc/
此示例比较远程文件时间戳以查找最旧的文件,修改它以将您的上次运行日期与远程文件日期进行比较,然后将下载作为循环的一部分进行。
来自Finding file size and last modified of SFTP file using Java的代码
try {
list = Main.chanSftp.ls("*.xml");
if (list.isEmpty()) {
fileFound = false;
}
else {
lsEntry = (ChannelSftp.LsEntry) list.firstElement();
oldestFile = lsEntry.getFilename();
attrs = lsEntry.getAttrs();
currentOldestTime = attrs.getMTime();
for (Object sftpFile : list) {
lsEntry = (ChannelSftp.LsEntry) sftpFile;
nextName = lsEntry.getFilename();
attrs = lsEntry.getAttrs();
nextTime = attrs.getMTime();
if (nextTime < currentOldestTime) {
oldestFile = nextName;
currentOldestTime = nextTime;
}
}
【讨论】: