Finding file size and last modified oldest SFTP file using Java
I am using JSch to get files from an SFTP server, but I am trying to find a way to get only the oldest file and make sure it is not currently being written. The way I picture myself is to first find which file in the specified remote folder is the oldest. Then I checked the file size, wait x seconds (maybe about 10 to be safe) and then check it again. If the file size hasn't changed, then I download the file and process it. However, I don't know how to do this! If anyone knows how to do this, or knows something else that supports SFTP that has this built in (I know what Apache Commons does, but only FTPS), it would be very helpful.
Thanks in advance.
a source to share
It turns out that this is quite possible in JSch, the hardest part is just finding the documentation. The code I used is below, hopefully someone else finds it useful! (I'm sure there are some optimizations, I know, I know. There are also variables that are defined elsewhere, but hopefully someone in need can figure them out!)
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;
}
a source to share
I don't have a direct answer to your question, but it looks like you want to do something similar to reliable file transfer . This is part of a larger Grid computing project that now appears to be organized here . I don't know if it includes security features or if you can add them, but this is an open source project.
a source to share
You can easily do this using edtFTPj / PRO which supports SFTP.
Just enter the directory list and sort the list by date. If the oldest date is not in the last few minutes, you can download it.
a source to share
calculate the size of a folder on a remote server, just call the directory path ftpFolderSize(ftpFolderSize,client)
and pass the object FTPClient
as a parameter. It will return the size of the folder.
Works for FTP only.
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.io.FileUtils;
long dirSize = 0L; //global variable
private long ftpFolderSize(String directoryPath,FTPClient client){
try{
client.changeWorkingDirectory(directoryPath);
FTPFile[] ftpFiles = client.listFiles();
if(client.changeWorkingDirectory(directoryPath))
{
for (FTPFile ftpFile : ftpFiles) {
if(ftpFile.isFile()){
dirSize = dirSize+ftpFile.getSize();// file size is calculated
}
else if(ftpFile.isDirectory())
{
dirSize = dirSize + 4096;//folder minimum size is 4kb
ftpFolderSize(directoryPath+"/"+ftpFile.getName(),client);
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
return dirSize;
}
a source to share