求助!Java里用jsch实现sftp时出现异常:com.jcraft.jsch.JSchException: failed to send channel r
xutsh 2011-01-26 04:04:54 大家好!
我想做的事情:
用java和jsch实现对远程sftp服务器的如下功能:
1,连接。
2,下载文件。
3,上传文件。
出现的问题:(详细信息在下面)
com.jcraft.jsch.JSchException: failed to send channel request
at com.jcraft.jsch.Request.write(Request.java:65)
at com.jcraft.jsch.RequestSftp.request(RequestSftp.java:47)
at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:190)
at com.jcraft.jsch.Channel.connect(Channel.java:200)
at com.jcraft.jsch.Channel.connect(Channel.java:144)
at com.xuts.test.sftp.util.SftpImplTest.connect(SftpImplTest.java:58)
at com.xuts.test.sftp.util.SftpImplTest.main(SftpImplTest.java:345)
出错地方:
public void connect() 方法里的这一句:
channel.connect();
public void connect() 方法:(详细代码在下面)
public void connect() {
try {
if (sftp != null) {
System.out.println("sftp is not null");
}
JSch.setLogger(new MyLogger());
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
System.out.println("Connect " + (sftp.isConnected()?"successed":"failed"));
} catch (Exception e) {
e.printStackTrace();
}
}
本地配置:
jdk版本:jdk1.5.0_22
jsch版本:jsch-0.1.44.jar
sftp服务器:
sftp版本:SSH-2.0-OpenSSH_5.3
控制台的信息:
Session created.
INFO: Connecting to 43.82.101.237 port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_5.3
INFO: Local version string: SSH-2.0-JSCH-0.1.44
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: aes256-ctr is not available.
INFO: aes192-ctr is not available.
INFO: aes256-cbc is not available.
INFO: aes192-cbc is not available.
INFO: arcfour256 is not available.
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server->client aes128-ctr hmac-md5 none
INFO: kex: client->server aes128-ctr hmac-md5 none
INFO: SSH_MSG_KEXDH_INIT sent
INFO: expecting SSH_MSG_KEXDH_REPLY
INFO: ssh_rsa_verify: signature true
WARN: Permanently added '43.82.101.237' (RSA) to the list of known hosts.
INFO: SSH_MSG_NEWKEYS sent
INFO: SSH_MSG_NEWKEYS received
INFO: SSH_MSG_SERVICE_REQUEST sent
INFO: SSH_MSG_SERVICE_ACCEPT received
INFO: Authentications that can continue: publickey,keyboard-interactive,password
Session connected.
Opening Channel.
INFO: Next authentication method: publickey
INFO: Authentications that can continue: keyboard-interactive,password
INFO: Next authentication method: keyboard-interactive
INFO: Authentications that can continue: password
INFO: Next authentication method: password
INFO: Authentication succeeded (password).
com.jcraft.jsch.JSchException: failed to send channel request
at com.jcraft.jsch.Request.write(Request.java:65)
at com.jcraft.jsch.RequestSftp.request(RequestSftp.java:47)
at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:190)
at com.jcraft.jsch.Channel.connect(Channel.java:200)
at com.jcraft.jsch.Channel.connect(Channel.java:144)
at com.xuts.test.sftp.util.SftpImplTest.connect(SftpImplTest.java:58)
at com.xuts.test.sftp.util.SftpImplTest.main(SftpImplTest.java:345)
localFile : D:\prod\metis_app\batch\scabinetbatch\test\1.txt
remotePath:/cygdrive/d/prod/metis_app/batch/scabinetbatch/test/1.txt
*******remotefile.getParent():\cygdrive\d\prod\metis_app\batch\scabinetbatch\test
*******create path failed\cygdrive\d\prod\metis_app\batch\scabinetbatch\test
Exception in thread "main" java.lang.NullPointerException
at com.xuts.test.sftp.util.SftpImplTest.upload(SftpImplTest.java:126)
at com.xuts.test.sftp.util.SftpImplTest.main(SftpImplTest.java:346)