[英]Determine latest file from SFTP server using Java JSch
有沒有辦法使用 Java JSch 確定 Unix SFTP 服務器上最新文件的名稱?
我想將最新的文件從服務器復制到本地機器。 我已經有一個工作代碼。 但我無法識別最新的文件。 該文件夾包含以下格式的許多文件:
Some Report dd/MM/yyyy hh:ss
我嘗試了這篇文章中提到的代碼,但它沒有選擇最新的文件。 此外,代碼似乎永遠不會停止執行。
任何幫助將不勝感激。
我的解決方案基於使用 Java 查找文件大小和上次修改的 SFTP 最舊文件中發布的代碼,並進行了以下修改:
nextTime
和currentOldestTime
的比較從if (nextTime < currentOldestTime)
更改為if (nextTime > currentOldestTime)
。 現在選擇最新的文件。下面是使用 jsch 將最小文件從遠程服務器復制到本地的工作程序:
package com.poc.client;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
public class CopyFileRemoteToLocal {
public static void main(String[] args) {
String hostname = "hostName";
String username = "userName";
String password = "password";
String copyFrom = "serverFilePath";
String copyTo = "LocalFilePath";
JSch jsch = new JSch();
Session session = null;
System.out.println("Trying to connect.....");
try {
session = jsch.getSession(username, hostname, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
Vector<LsEntry> vector = (Vector<LsEntry>) sftpChannel.ls(copyFrom);
ChannelSftp.LsEntry list = vector.get(0);
String oldestFile =list.getFilename();
SftpATTRS attrs=list.getAttrs();
int currentOldestTime =attrs.getMTime();
String nextName=null;
LsEntry lsEntry=null;
int nextTime;
for (Object sftpFile : vector) {
lsEntry = (ChannelSftp.LsEntry) sftpFile;
nextName = lsEntry.getFilename();
attrs = lsEntry.getAttrs();
nextTime = attrs.getMTime();
if (nextTime > currentOldestTime) {
oldestFile = nextName;
currentOldestTime = nextTime;
}
}
System.out.println("File name is ...."+oldestFile);
sftpChannel.get(copyFrom+oldestFile, copyTo);
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
System.out.println("Done !!");
}
}
根據你提到這個帖子的帖子
有代碼行說只過濾 xml 文件你有沒有改變它來檢查所有文件格式
list = Main.chanSftp.ls("*.xml");
還有在執行線程時調用了 sleep 方法 1 分鍾
Thread.sleep(60000);
所以希望代碼至少運行一分鍾
這可能有幫助
ChannelSftp.LsEntry lastModifiedEntry = Collections.max(list, (Comparator.comparatorInt(character1 -> character1.getAttrs().getMTime()) .thenComparingInt(character2 -> character2.getAttrs().getMTime())));
您可以獲取文件夾中最后修改的條目。
它可以通過集合或流來完成,因為 channelSftp.ls 返回向量的 LsEntry 並且 Vector.class 與 Java 集合完全兼容:
Vector<LsEntry> list = channelSftp.ls(filePath + "*.txt");
ChannelSftp.LsEntry lastModifiedEntry = Collections.max(list,
(Comparator.comparingInt(entry-> entry.getAttrs().getMTime()))
);
或者
LsEntry lastModifiedEntry = list.stream().max(
Comparator.comparingInt(entry -> entry.getAttrs().getMTime())
).get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.