将文件上传到 sftp(私钥身份验证)时 JSch 中出现失败错误
我正在尝试在私钥身份验证后使用 JSch 库将文件上传到 sftp 服务器。但是 put、mkdir 等操作会抛出失败消息。我尝试了几种方法,确实看起来像是 JSch 中的问题,有人遇到过这个问题吗?
相同的代码(mkdir,put)非常适合非私钥(用户名/密码)身份验证。请你帮助我好吗。 sftpChannel.ls 也采用私钥方法。
我的代码:
JSch jsch = new JSch();
Session session = null;
if (privateKey != null||"".equals(privateKey)) {
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, hostAddress, port);
session.setConfig("StrictHostKeyChecking", "no");
if(password!=null||"".equals(password)){
session.setPassword(password);
}
session.connect();
Channel channel = null;
if (sftp) {
channel = session.openChannel("sftp");
} else {
channel = session.openChannel("ftp");
}
session.setServerAliveInterval(92000);
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
// sftpChannel.put(new FileInputStream(new File(path)), remotePath );
// final Vector files = sftpChannel.ls(".");
// for (Object obj : files) {
// System.out.println("f:"+obj);
// }
sftpChannel.exit();
session.disconnect();
Exception in thread "main" 4: Failure
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2297)
at com.jcraft.jsch.ChannelSftp.mkdir(ChannelSftp.java:1708)
at zz.beans.RemoteExportBean.exportToFTP(RemoteExportBean.java:52)
at zz.beans.RemoteExportBean.main(RemoteExportBean.java:67)
Exception in thread "main" 4: Failure
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2297)
at com.jcraft.jsch.ChannelSftp.checkStatus(ChannelSftp.java:1946)
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:566)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:438)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:405)
at zz.beans.RemoteExportBean.exportToFTP(RemoteExportBean.java:52)
at zz.beans.RemoteExportBean.main(RemoteExportBean.java:66)
I'm trying to upload a file to a sftp server using JSch libs after private key authentication. But put,mkdir etc actions are throwing failure messages. I tried several ways, sure it looks like a problem in JSch, is any one come across this problem?
The same code(mkdir,put) is working perfect for non private key (username/password) authentication. Could you please help me. sftpChannel.ls is also working in private key approach.
My code:
JSch jsch = new JSch();
Session session = null;
if (privateKey != null||"".equals(privateKey)) {
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, hostAddress, port);
session.setConfig("StrictHostKeyChecking", "no");
if(password!=null||"".equals(password)){
session.setPassword(password);
}
session.connect();
Channel channel = null;
if (sftp) {
channel = session.openChannel("sftp");
} else {
channel = session.openChannel("ftp");
}
session.setServerAliveInterval(92000);
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
// sftpChannel.put(new FileInputStream(new File(path)), remotePath );
// final Vector files = sftpChannel.ls(".");
// for (Object obj : files) {
// System.out.println("f:"+obj);
// }
sftpChannel.exit();
session.disconnect();
Exception in thread "main" 4: Failure
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2297)
at com.jcraft.jsch.ChannelSftp.mkdir(ChannelSftp.java:1708)
at zz.beans.RemoteExportBean.exportToFTP(RemoteExportBean.java:52)
at zz.beans.RemoteExportBean.main(RemoteExportBean.java:67)
Exception in thread "main" 4: Failure
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2297)
at com.jcraft.jsch.ChannelSftp.checkStatus(ChannelSftp.java:1946)
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:566)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:438)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:405)
at zz.beans.RemoteExportBean.exportToFTP(RemoteExportBean.java:52)
at zz.beans.RemoteExportBean.main(RemoteExportBean.java:66)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论