SSHJ:如何使用StatefulSFTPClient

时间:2018-05-08 08:19:12

标签: java sshj

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

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

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

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

1 个答案:

答案 0 :(得分:0)

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

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");
            }