使用 Java 查找 SFTP 最旧文件的文件大小和最后修改时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2641286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding file size and last modified of SFTP oldest file using Java
提问by ryantmer
I'm using JSch to get files from an SFTP server, but I'm trying to figure out a way to only get the oldest file, and to make sure that it is not currently being written to. The way I imagine myself doing this is first finding which file in the specified remote folder is oldest. I would then check the file size, wait x seconds (probably about 10, just to be safe) and then check it again. If the file size has not changed, I download the file and process it. However, I have no idea how to do this! If anybody knows how to do this, or knows of something else that supports SFTP that has this built-in (I know Apache Commons does, but only does FTPS), it would be greatly appreciated.
我正在使用 JSch 从 SFTP 服务器获取文件,但我试图找出一种方法来只获取最旧的文件,并确保它当前没有被写入。我想象自己这样做的方式是首先查找指定远程文件夹中的哪个文件最旧。然后我会检查文件大小,等待 x 秒(可能大约 10 秒,只是为了安全)然后再次检查。如果文件大小没有改变,我会下载文件并进行处理。但是,我不知道该怎么做!如果有人知道如何做到这一点,或者知道其他支持 SFTP 的东西(我知道 Apache Commons 可以,但只支持 FTPS),我们将不胜感激。
Thanks in advance.
提前致谢。
采纳答案by ryantmer
Turns out that this is entirely possible in JSch, the hardest part is simply finding the documentation. Code I used is below, hopefully somebody else will find it helpful! (I'm sure there are optimizations to be made, I know, I know. There are also variables that are defined elsewhere, but hopefully anybody that needs this will be able to figure them out!)
事实证明,这在 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;
}
回答by President James K. Polk
I don't have a direct answer to your question, but it sounds like you want to do something similar to reliable file transfer. This is part of a larger project in Grid computing that is now apparently organized here. I don't know if it includes security features or if you can add them on, but it is an open source project.
我没有直接回答你的问题,但听起来你想做一些类似于可靠文件传输的事情。这是一个更大的网格计算项目的一部分,现在显然组织在这里。我不知道它是否包含安全功能,或者您是否可以添加它们,但它是一个开源项目。
回答by Bruce Blackshaw
You can easily do this using edtFTPj/PRO, which supports SFTP.
您可以使用支持 SFTP 的edtFTPj/PRO轻松完成此操作。
Simply get a directory listing, and sort the listing by date. If the oldest date isn't in the last few minutes, you can download.
只需获取目录列表,然后按日期对列表进行排序。如果最旧的日期不在最近几分钟内,您可以下载。
回答by Arundev
calculate the folder size in remote server just call
the ftpFolderSize(ftpFolderSize,client)
directory path, and
pass the object FTPClient
as a parameter. It will return the
size of folder.
计算远程服务器中的文件夹大小只需调用ftpFolderSize(ftpFolderSize,client)
目录路径,并将对象FTPClient
作为参数传递。它将返回文件夹的大小。
Works only for FTP.
仅适用于 FTP。
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;
}