简体   繁体   English

Apache VFS:获取目录的最新更改文件(SFTP)

[英]Apache vfs: fetch latest changed file of a directory (sftp)

I am trying to pull the latest file out of a directory, which is located on a sftp server. 我正在尝试从位于sftp服务器上的目录中提取最新文件。 The way I do it right now is more or less: 我现在的做法或多或少:

public FileObject getLatestFile(String directory) throws FileSystemException {
    FileObject fo = fsManager.resolveFile(this.host+directory, fsOptions);
    FileObject latestFile = null;
    long max  = 0;
    fo.getContent().
    for (FileObject fob : fo.getChildren()){
        if (fob.getContent().getLastModifiedTime() > max) {
            max = fob.getContent().getLastModifiedTime();
            latestFile = fob;
        }
    }
    return latestFile;
}

The problem with this approach is that I am basically downloading every file in the given directory, everytime the method is called. 这种方法的问题是,每次调用该方法时,我基本上都会下载给定目录中的每个文件。

Is there any better way to do this? 有什么更好的方法吗?

You are not downloading the content. 您不下载内容。

If you look in the source code: 如果您查看源代码:

/**
 * Returns the file's content.
 */
public FileContent getContent() throws FileSystemException
{
    synchronized (fs)
    {
        attach();
        if (content == null)
        {
            content = new DefaultFileContent(this, getFileContentInfoFactory());
        }
        return content;
    }
}

calling getContent just returns an object implementation and getting attributes like size, modified dates basically it is extracted when exploring the remote folder(every protocol is different, but for example when you list an FTP folder you get all the files attributes). 调用getContent只是返回一个对象实现,并获取诸如大小,修改日期之类的属性,基本上是在浏览远程文件夹时提取的(每个协议都不同,但是例如,当您列出一个FTP文件夹时,您会获得所有文件属性)。

For SFTP this what you actually call : 对于SFTP,您实际上称之为:

protected long doGetLastModifiedTime() throws Exception
{
    if (attrs == null
            || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) == 0)
    {
        throw new FileSystemException(
                "vfs.provider.sftp/unknown-modtime.error");
    }
    return attrs.getMTime() * 1000L;
}

I agree, naming is confusing and it implies that content is retrieved when getContent is invoked but actually is not. 我同意,命名令人困惑,这意味着在调用getContent时会检索内容,但实际上并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM