Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.jcraft.jsch.*;
- import java.io.*;
- import java.io.IOException;
- /**
- *
- * @author anand
- * @source http://wiki.jsch.org/index.php?Manual%2FExamples%2FSftpFileCopyExample
- */
- public class sftpex {
- public static void main(String[] args) {
- // TODO code application logic here
- String username = "username";
- String host = "localhost";
- String pass = "password";
- //String khfile = "/home/testuser/.ssh/known_hosts";
- //String identityfile = "/home/testuser/.ssh/id_rsa";
- JSch jsch = null;
- Session session = null;
- Channel channel = null;
- ChannelSftp c = null;
- try {
- jsch = new JSch();
- session = jsch.getSession(username, host, 22);
- session.setPassword(pass);
- //jsch.setKnownHosts(khfile);
- //jsch.addIdentity(identityfile);
- java.util.Properties config = new java.util.Properties();
- config.put("StrictHostKeyChecking", "no");
- session.setConfig(config);
- session.connect();
- //jsch.setConfig("StrictHostKeyChecking", "no");
- channel = session.openChannel("sftp");
- channel.connect();
- c = (ChannelSftp) channel;
- } catch (Exception e) { e.printStackTrace(); }
- try {
- System.out.println("Starting File Upload:");
- String src = "D:/Varun/SCP/1.txt";
- String dest = "C:/Documents and Settings/Administrator/SCPDemo/test123.txt";
- c.put(src, dest);
- //c.get(fdest, "/tmp/testfile.bin");
- } catch (Exception e) { e.printStackTrace(); }
- c.disconnect();
- session.disconnect();
- }
- }
- //When I try to run this
- javac .;path/to/jsch.0.1.5.0.jar sftpex.java
- java .;path/to/jsch.0.1.5.0.jar sftpex
- I get the following error message.
- Starting File Upload:
- 4: The requested operation failed.
- at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2833)
- at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:594)
- at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:475)
- at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:365)
- at sftpex.main(sftpex.java:47)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement