java sftp ls_使用Java查找SFTP文件的文件大小和上次修改

事实证明,这在JSch中是完全可能的,最难的部分就是找到文档。我使用的代码如下,希望其他人会发现它有用! (我知道有优化可以做,我知道,我知道。还有其他地方定义的变量,但希望任何需要它的人都可以解决它们!)

public static String oldestFile() {

Vector list = null;

int currentOldestTime;

int nextTime = 2140000000; //Made very big for future-proofing

ChannelSftp.LsEntry lsEntry = null;

SftpATTRS attrs = null;

String nextName = null;

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;

}

}

attrs = chanSftp.lstat(Main.oldestFile);

long size1 = attrs.getSize();

System.out.println("-Ensuring file is not being written to (waiting 1 minute)");

Thread.sleep(60000); //Wait a minute to make sure the file size isn't changing

attrs = chanSftp.lstat(Main.oldestFile);

long size2 = attrs.getSize();

if (size1 == size2) {

System.out.println("-It isn't.");

fileFound = true;

}

else {

System.out.println("-It is.");

fileFound = false;

}

}

} catch (Exception ex) {ex.printStackTrace();}

return Main.oldestFile;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值