使用 Jsch 抛出 NullPointer 连接到 SFTP

问题描述 投票:0回答:1

我正在使用 jsch 编写一个基本应用程序来尝试连接 AWS SFTP 服务器。

我使用的是jsch版本

implementation group: 'com.github.mwiede', name: 'jsch', version: '0.2.11'

当我尝试建立连接时,握手后它会立即断开连接,并且我的控制台显示此错误代码。

com.jcraft.jsch.JSchException: java.lang.NullPointerException
    at com.jcraft.jsch.Session.receive_kexinit(Session.java:605)

用这个简单的连接代码

try{
    Security.insertProviderAt(new BouncyCastleProvider(), 1);

    Hashtable<String, String> config = new Hashtable<>();
    config.put("StrictHostKeyChecking", "no");

    JSch sch = new JSch();
    JSch.setConfig(config);
    JSch.setLogger(new TestLogger(logger));

    JSch.setConfig("kex", "diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256");
    JSch.setConfig("mac.s2c", "[email protected],[email protected]");
    JSch.setConfig("mac.c2s", "[email protected],[email protected]");

    JSch.setConfig("enable_server_sig_algs", "yes");
    JSch.setConfig("prefer_known_host_key_types", "yes");
    JSch.setConfig("PubkeyAcceptedAlgorithms", "ssh-rsa");

    Session session = sch.getSession(username, hostname, 22);

    system.print.out.ln("Opening session for SFTP");
    sch.addIdentity(rsaKey);

    System.print.out.ln("Attempting to connect");
    session.connect(60 * 1000);

}catch (Exception e){
    System.print.out.ln("Error: " + e);
}

下面是连接期间发生的情况的信息


Attempting to connect
INFO | Connecting to HOSTNAME port PORT
INFO | Connection established
INFO | Remote version string: SSH-2.0-AWS_SFTP_1.1
INFO | Local version string: SSH-2.0-JSCH-0.1.54
INFO | CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO | SSH_MSG_KEXINIT sent
INFO | SSH_MSG_KEXINIT received
INFO | kex: server: diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
INFO | kex: server: rsa-sha2-512,rsa-sha2-256,ssh-rsa
INFO | kex: server: [email protected],[email protected],aes256-ctr,aes192-ctr
INFO | kex: server: [email protected],[email protected],aes256-ctr,aes192-ctr
INFO | kex: server: [email protected],hmac-sha2-[email protected]
INFO | kex: server: [email protected],[email protected]
INFO | kex: server: none,[email protected]
INFO | kex: server: none,[email protected]
INFO | kex: server: 
INFO | kex: server: 
INFO | kex: client: diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
INFO | kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO | kex: client: aes192-ctr
INFO | kex: client: aes192-ctr
INFO | kex: client: [email protected],[email protected]
INFO | kex: client: [email protected],[email protected]
INFO | kex: client: none
INFO | kex: client: none
INFO | kex: client: 
INFO | kex: client: 
INFO | kex: server->client aes192-ctr [email protected] none
INFO | kex: client->server aes192-ctr hmac-sha2-256-et[email protected] none
INFO | Disconnecting from HOSTNAME port PORT
java sftp jsch
1个回答
0
投票

非常感谢dave_tompson_085Rob Spoor。我查看了我的依赖项 build.gradle,发现由于某种原因,我的另一个模块在 build.gradle 中包含了 jcraft.jsch 0.1.54 库。我删除了它,现在我可以建立 sftp 连接。

© www.soinside.com 2019 - 2024. All rights reserved.