0

我正在尝试连接到 SFTP 服务器,在我家的另一台笔记本电脑上运行(在 win 10 上运行)我正在使用SSHJ库。当我使用带密码的身份验证时,它工作正常:

client.authPassword("myUserName", "myPassword") 

但是当我尝试使用密钥进行身份验证时,我收到异常“用尽可用的身份验证方法”我不确定是否需要使用私钥或公钥,所以我尝试了两者(取自 SFTP 服务器),都返回同样的错误。这是我的代码:

public static void sftpUsingKey() throws IOException {
    final SSHClient client = new SSHClient();
    client.loadKnownHosts();
    try {
        client.addHostKeyVerifier(new PromiscuousVerifier());
        client.connect(remoteHost);
        PKCS8KeyFile keyfile = new PKCS8KeyFile();
        File privateKeyFile = new File("path/to/key");
        keyfile.init(privateKeyFile);
        client.authPublickey("myUserName", keyfile);

        final SFTPClient sftp = client.newSFTPClient();
        try {
            sftp.put(new FileSystemFile(src+fileName), remoteDst);
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
        finally {
            sftp.close();
        }
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    } finally {
        client.disconnect();
    }
}

任何提示我做错了什么?

4

0 回答 0