1

I am writing an SFTP client using the Apache Mina SSHD library, specifically sshd-sftp 2.3.0. I chose it instead of the SSHJ library, because it is possible to write a String to a remote file directly, and there are also no unnecessary dependencies.

The task: when sending files to the SFTP server to make a backup copy of the previous file, if any.

I studied all the possibilities that the SftpClient class provides, and among them I did not find an easy way to search for a file.

  • There are listDir and readDir methods, they return Iterable <DirEntry>, but I suspect that with a large number of files they will work slowly.
  • We can simply force a file to be renamed using the rename method, but in my case there can be many backup files, and we must first find a free file name.

The current solution is to get the state of the file. If there is no such file, then a SftpException will be thrown, for example, "SFTP error (SSH_FX_NO_SUCH_FILE): No such file or directory". This message can be analyzed, and if the file is really not found, then just don't throw this exception further. Here is the code:

    private boolean fileExists(SftpClient sftpClient, String remoteDest) throws IOException {
        try {
            return null != sftpClient.stat(remoteDest);
        } catch (SftpException e) {
            if (e.getStatus() != SftpConstants.SSH_FX_NO_SUCH_FILE) {
                LOGGER.error("Error finding file {}", remoteDest);
                throw e;
            }
            return false;
        }
    }

The main problem is that this solution is ugly.

However, on the Internet I found only similar solutions using libraries JSch and Trilead SSH-2 for Java.

Perhaps there is another, simpler and more competent way to check for a file on an SFTP server?

4

1 回答 1

1

那是正确的解决方案。

SFTP 协议没有“文件是否存在”查询/请求。最接近的等价物实际上是“stat”查询/请求。因此,即使您最喜欢的 SFTP 库具有“文件是否存在”方法/API,它也会在内部执行您的代码执行的操作。

JSch 中的等价物:使用 JSch,有没有办法在不执行 ls 的情况下判断远程文件是否存在?

于 2019-09-06T10:33:02.773 回答