I am trying to upload a file to an sftp server using JSch libraries after authentication using a secret key. But put, mkdir, etc. Actions throw error messages. I tried several ways, I'm sure this seems like a problem in JSch, is there anyone running into this problem?
The same code (mkdir, put) works ideally for authentication without a private key (username / password). Could you help me. sftpChannel.ls also works with a private key.
My code is:
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)
source share