Advertisement
Varun_Krishna

Transferring files to remote host using SFTP using Jsch.jar

Feb 28th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import com.jcraft.jsch.*;
  2. import java.io.*;
  3. import java.io.IOException;
  4.  
  5. /**
  6. *
  7. * @author anand
  8. * @source http://wiki.jsch.org/index.php?Manual%2FExamples%2FSftpFileCopyExample
  9. */
  10. public class sftpex {
  11.    public static void main(String[] args) {
  12.        // TODO code application logic here
  13.  
  14.        String username = "username";
  15.        String host = "localhost";
  16.        String pass = "password";
  17.        //String khfile = "/home/testuser/.ssh/known_hosts";
  18.        //String identityfile = "/home/testuser/.ssh/id_rsa";
  19.        
  20.  
  21.        JSch jsch = null;
  22.        Session session = null;
  23.        Channel channel = null;
  24.        ChannelSftp c = null;
  25.        try {
  26.            jsch = new JSch();
  27.            session = jsch.getSession(username, host, 22);
  28.            session.setPassword(pass);
  29.            //jsch.setKnownHosts(khfile);
  30.            //jsch.addIdentity(identityfile);
  31.            java.util.Properties config = new java.util.Properties();
  32.             config.put("StrictHostKeyChecking", "no");
  33.             session.setConfig(config);
  34.            session.connect();
  35.            //jsch.setConfig("StrictHostKeyChecking", "no");
  36.  
  37.            channel = session.openChannel("sftp");
  38.            channel.connect();
  39.            c = (ChannelSftp) channel;
  40.  
  41.        } catch (Exception e) {  e.printStackTrace();    }
  42.  
  43.        try {
  44.            System.out.println("Starting File Upload:");
  45.            String src = "D:/Varun/SCP/1.txt";
  46.            String dest = "C:/Documents and Settings/Administrator/SCPDemo/test123.txt";
  47.            c.put(src, dest);
  48.  
  49.            //c.get(fdest, "/tmp/testfile.bin");
  50.        } catch (Exception e) {  e.printStackTrace();    }
  51.  
  52.        c.disconnect();
  53.        session.disconnect();       
  54.    }
  55. }
  56.  
  57.  
  58. //When I try to run this
  59. javac .;path/to/jsch.0.1.5.0.jar sftpex.java
  60. java .;path/to/jsch.0.1.5.0.jar sftpex
  61. I get the following error message.
  62. Starting File Upload:
  63. 4: The requested operation failed.
  64.         at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2833)
  65.         at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:594)
  66.         at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:475)
  67.         at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:365)
  68.         at sftpex.main(sftpex.java:47)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement