CLICK HERE TO EARN MONEY FREE

Saturday, March 7, 2015

SFTP USING JSCH - File transfer

file transfer can be done by many way . i used here jsch jar.to establish a connection with remote server .transfer file from my local machine to remote machine

import java.io.File;
import java.io.FileInputStream;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.*;
public class newtest {
   
    public static void main(String args[])
    {
    try {
        String ftpHost = "servername";  //note:please change server name .other wise it wont work
        int ftpPort = 22;    
        String ftpUserName = "username";  // username here
        String ftpPassword = "password";  // password here
        String ftpRemoteDirectory = "/";
        String fileToTransmit = "E:/windowslocal/hello.txt";         
       // JSch.setLogger(new MyLogger());
        System.out.println("Creating session.");
        JSch jsch = new JSch();

        Session session = null;
        Channel channel = null;
        ChannelSftp c = null;

        //
        // Now connect and SFTP to the SFTP Server
        //
        try {
            // Create a session sending through our username and password
            session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
            System.out.println("Session created.");
            session.setPassword(ftpPassword);

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            System.out.println("Session connected before.");
            session.connect();
            System.out.println("Session connected.");

            System.out.println("OPEN SFTP CHANNEL");
            //
            // Open the SFTP channel
            //
            System.out.println("Opening Channel.");
            channel = session.openChannel("sftp");
            channel.connect();
            c = (ChannelSftp) channel;
            System.out.println("Now checing status");
        } catch (Exception e) {
            System.err.println("Unable to connect to FTP server."
                    + e.toString());
            throw e;
        }

        //
        // Change to the remote directory
        //
        System.out.println("Now performing operations");
        ftpRemoteDirectory="/home/azureuser/";
        System.out.println("Changing to FTP remote dir: "
                + ftpRemoteDirectory);
        c.cd(ftpRemoteDirectory);
       
        //
        // Send the file we generated
        //
        try {
            File f = new File(fileToTransmit);
            System.out.println("Storing file as remote filename: "
                    + f.getName());
            c.put(new FileInputStream(f), f.getName());
        } catch (Exception e) {
            System.err
                    .println("Storing remote file failed." + e.toString());
            throw e;
        }  

       
       
        //
        // Disconnect from the FTP server
        //
        try {
           
            c.quit();
        } catch (Exception exc) {
            System.err.println("Unable to disconnect from FTPserver. "
                    + exc.toString());
        }

    } catch (Exception e) {
        System.err.println("Error: " + e.toString());
       
       
    }
   
   
    System.out.println("Process Complete.");
    System.exit(0);
}

}

0 comments:

Post a Comment

 

TEKONOLOGY Copyright © 2011 -- Template created by TEKONOLOGY -- Powered by Blogger