com.jcraft.jsch.JSchException:java.io.IOException:通道已损坏

时间:2018-05-01 06:46:27

标签: java sftp jsch

尝试连接到sftp时出现com.jcraft.jsch.JSchException: java.io.IOException: channel is broken异常。

Maven依赖:

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
</dependency>

错误堆栈跟踪:

com.jcraft.jsch.JSchException: java.io.IOException: channel is broken
    at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:315)
    at com.jcraft.jsch.Channel.connect(Channel.java:152)
    at com.toyota..XmlSftpClientJob.connectPsa(XmlSftpClientJob.java:104)
    at com..XmlSftpClientJob.main(XmlSftpClientJob.java:47)
    at com..XmlSftpClientJob.main(XmlSftpClientJob.java:181)
Caused by: java.io.IOException: channel is broken
    at com.jcraft.jsch.Session.write(Session.java:1289)
    at com.jcraft.jsch.ChannelSftp.sendINIT(ChannelSftp.java:2499)
    at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:258)
    ... 4 more

我的代码:

public ChannelSftp connectPsa(HostKey hostKey) throws JSchException {
    ChannelSftp sftp = new ChannelSftp();
    JSch jsch = new JSch();
    //jsch.getHostKeyRepository().add(hostKey, null);
    Session sshSession = jsch.getSession("username", "hostname", 22);
    System.out.println("Session created. ");
    sshSession.setPassword("password");
    Properties sshConfig = new Properties();
    sshConfig.put("StrictHostKeyChecking", "no");
    sshSession.setConfig(sshConfig);
    sshSession.setTimeout(5000);
    sshSession.setConfig("kex", "diffie-hellman-group1-sha1");
    sshSession.connect();
    int val = sshSession.getServerAliveInterval();
    sshSession.setServerAliveInterval(10000);
    System.out.println("Session connected.");
    System.out.println("Opening Channel.");
    sftp = (ChannelSftp)sshSession.openChannel("sftp");
    //ChannelSftp channelSftp =  channel;
    sftp.connect(1000);
    System.out.println("Connected to " + "hostname" + ".");
    return sftp;
}


`Added Detailed Jsch log::  `

Session created. 
Connecting to sftp_hostname 22
Connection established
Remote version string: SSH-2.0-Maverick_SSHD
Local version string: SSH-2.0-JSCH-0.1.54
CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
aes256-ctr is not available.
aes192-ctr is not available.
aes256-cbc is not available.
aes192-cbc is not available.
CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
diffie-hellman-group14-sha1 is not available.
CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
SSH_MSG_KEXINIT sent
SSH_MSG_KEXINIT received
kex: server: diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1
kex: server: ssh-rsa
kex: server: aes256-cbc,aes192-cbc,aes128-cbc,blowfish-cbc
kex: server: aes256-cbc,aes192-cbc,aes128-cbc,blowfish-cbc
kex: server: hmac-sha2-256,hmac-sha1-96,hmac-sha1
kex: server: hmac-sha2-256,hmac-sha1-96,hmac-sha1
kex: server: none
kex: server: none
kex: server: 
kex: server: 
kex: client: diffie-hellman-group1-sha1
kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: none
kex: client: none
kex: client: 
kex: client: 
kex: server->client aes128-cbc hmac-sha1 none
kex: client->server aes128-cbc hmac-sha1 none
SSH_MSG_KEXDH_INIT sent
expecting SSH_MSG_KEXDH_REPLY
ssh_rsa_verify: signature true
Permanently added 'Hostname' (RSA) to the list of known hosts.
SSH_MSG_NEWKEYS sent
SSH_MSG_NEWKEYS received
SSH_MSG_SERVICE_REQUEST sent
SSH_MSG_SERVICE_ACCEPT received
Authentications that can continue: publickey,keyboard-interactive,password
Next authentication method: publickey
Authentications that can continue: keyboard-interactive,password
Next authentication method: keyboard-interactive
Authentication succeeded (keyboard-interactive).
Session connected.
Opening Channel.
Caught an exception, leaving main loop due to SSH_MSG_DISCONNECT: 11 User Disconnected 
Disconnecting from hostname port 22
com.jcraft.jsch.JSchException: java.io.IOException: channel is broken
    at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:315)
    at com.jcraft.jsch.Channel.connect(Channel.java:152)
    at com.....SftpClientJob.connect(XmlSftpClientJob.java:127)
    at com.....XmlSftpClientJob.main(XmlSftpClientJob.java:51)
    at com......XmlSftpClientJob.main(XmlSftpClientJob.java:204)
Caused by: java.io.IOException: channel is broken
    at com.jcraft.jsch.Session.write(Session.java:1289)
    at com.jcraft.jsch.ChannelSftp.sendINIT(ChannelSftp.java:2499)
    at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:258)
    ... 4 more

1 个答案:

答案 0 :(得分:0)

服务器端出错,因此连接突然终止。

所以Jsch没问题。现在工作正常。