SSHJ:如何使用 StatefulSFTPClient

SSHJ: How to use StatefulSFTPClient

我很困惑...我正在使用 com/hierynomus/sshj SFTP-lib,下面的代码给了我 ClassCastException:

SSHClient ssh = new SSHClient();
//... connect etc
StatefulSFTPClient client = (StatefulSFTPClient) ssh.newSFTPClient();

我需要 StatefulSFTPClient 但我不知道如何使用它...我需要扩展 SSHClient class 吗:

public StatefulSFTPClient newStatefulSFTPClient()
        throws IOException {
    checkConnected();
    checkAuthenticated();
    return new StatefulSFTPClient(new SFTPEngine(this).init());
}

哦,天哪...不是最亮的时刻 - 可以这样做:

StatefulSFTPClient client = new StatefulSFTPClient(new SFTPEngine(ssh).init());

并模仿 SSHClient.newSFTPClient():

            StatefulSFTPClient client;          
            if(ssh.isConnected()){
                if(ssh.isAuthenticated()){
                    client = new StatefulSFTPClient(new SFTPEngine(ssh).init());
                } else {
                    throw new IllegalStateException("Not authenticated");
                }
            } else {
                throw new IllegalStateException("Not connected");
            }