Reputation: 453
I have lots of file on server named like:
STMTPULL.160618_030020
O48PRMBATCH_170618_070209
O48PRMBATCH_160618_030106
STMTPOST.160618_030020
I want to use regex to locate the newest O48PRMBATCH*
Now I only can download file with an actual file name. This is not enough now.
How can I change my code to implement this feature? Thank you.
Here is my code (Code has been updated, it works fine):
public class DownloadFile {
public SFTPChannel getSFTPChannel() {
return new SFTPChannel();
}
public void downloadFile(String srcPath, String dstPath, String fileName) throws Exception {
DownloadFile test = new DownloadFile();
Map<String, String> sftpDetails = new HashMap<String, String>();
// Set host, port, user name, password
sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "172.16.1.180");
sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, "v01uc");
sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, "v01uc");
sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, "22");
SFTPChannel channel = test.getSFTPChannel();
ChannelSftp chSftp = channel.getChannel(sftpDetails, 60000);
String src = srcPath + fileName;
Vector<ChannelSftp.LsEntry> obj = chSftp.ls(src);
ArrayList<ChannelSftp.LsEntry> iList = new ArrayList<ChannelSftp.LsEntry>();
for (int i = 0; i < obj.size(); i++){
iList.add(obj.get(i));
}
Collections.sort(iList);
int index = obj.size() - 1;
String sFile = srcPath + iList.get(index).getFilename();
String dst = dstPath + iList.get(index).getFilename();
OutputStream out = new FileOutputStream(dst);
try {
chSftp.get(sFile, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
chSftp.quit();
channel.closeChannel();
}
}
}
Upvotes: 1
Views: 2672
Reputation: 202310
There's no function to download files matching a regular expression in JSch.
You have to code it yourself.
ChannelSftp.ls
to retrieve a list of the files;Upvotes: 1