Manual

A quick and dirty example of using JSCH library to transfer files to/from a server.

import com.jcraft.jsch.*;
import java.io.*;
import java.io.IOException;

/**
*
* @author anand
*/
public class sftpex {
   public static void main(String[] args) {
       // TODO code application logic here

       String username = "testuser";
       String host = "testserver.example.com";
       String pass = "testpass";
       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);
           session.connect();

           channel = session.openChannel("sftp");
           channel.connect();
           c = (ChannelSftp) channel;

       } catch (Exception e) { 	e.printStackTrace();	}

       try {
           System.out.println("Starting File Upload:");
           String fsrc = "/tmp/abc.txt", fdest = "/tmp/cde.txt";
           c.put(fsrc, fdest);
       
           c.get(fdest, "/tmp/testfile.bin");
       } catch (Exception e) {	e.printStackTrace();	}
       
       c.disconnect();
       session.disconnect();

   }
}

Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Last-modified: 2008-03-14 (Fri) 09:28:46 (2377d)