java – 如何在使用Apache VFS时在JSch Session上设置KEX

我正在使用Apache VFS 2.1和JSch 0.1.54(今天的最新版本)来使用SFTP上传文件.

连接时系统抛出“IO流读取结束”,完整堆栈为:

Caused by: com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Read
    at com.jcraft.jsch.Session.connect(Session.java:565)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:166)

我在this thread中找到了解决问题的可能方法,但它直接与JSch一起运行.由于我使用Apache VFS,我无法直接访问Session(至少我认为我不能).

但如果可以,它看起来像这样:

session.setConfig("kex", "diffie-hellman-group1-sha1");

查看Apache VFS实现org.apache.commons.vfs2.provider.sftp.SftpClientFactory,它没有找到设置此属性的方法.

如何使用Apache VFS API设置此配置?

更新

日志数据:

22/05/2017 09:27:20 org.apache.commons.vfs2.impl.StandardFileSystemManager info
INFO: Using "D:\Users\TECBMEPI\AppData\Local\Temp\vfs_cache" as temporary files store.
22/05/2017 09:27:20 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: Connecting to z1t1brmxpta022 port 22
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: Connection established
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: Remote version string: SSH-2.0-OpenSSH_6.6.1
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: Local version string: SSH-2.0-JSCH-0.1.54
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: aes256-ctr is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: aes192-ctr is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: aes256-cbc is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: aes192-cbc is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: diffie-hellman-group14-sha1 is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: ecdh-sha2-nistp256 is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: ecdh-sha2-nistp384 is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: ecdh-sha2-nistp521 is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: ecdsa-sha2-nistp256 is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: ecdsa-sha2-nistp384 is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: ecdsa-sha2-nistp521 is not available.
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: SSH_MSG_KEXINIT sent
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: SSH_MSG_KEXINIT received
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ssh-ed25519
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: none,zlib@openssh.com
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: none,zlib@openssh.com
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: 
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server: 
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: ssh-rsa,ssh-dss
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: none
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: none
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: 
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client: 
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: server->client aes128-ctr hmac-md5 none
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: kex: client->server aes128-ctr hmac-md5 none
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: SSH_MSG_KEX_DH_GEX_REQUEST(1024<1024<1024) sent
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: expecting SSH_MSG_KEX_DH_GEX_GROUP
22/05/2017 09:27:21 org.apache.commons.vfs2.provider.sftp.SftpClientFactory log
INFO: Disconnecting from z1t1brmxpta022 port 22

最佳答案 JSch会话从其JSch实例继承设置.并且所有JSch实例实际上都与设置共享相同的全局/静态哈希表.

所以只需在SftpClientFactory.createConnection之前调用static JSch.setConfig

JSch.setConfig("kex", "diffie-hellman-group1-sha1");
点赞