Wednesday, October 9, 2013

SFTP connection using Java for remote file transfer

SFTP Connection using Java for remote file transfer

I had created one main class in java for reading a csv file from SFTP server to local system and then upload a csv file from local system to SFTP server using JSch Library.

Pre-requisite: 

  • Download Jsch library from here.
  • Download Java CSV library from here.


Code : 

Please find my class below


package com.cybage.connection;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import com.csvreader.CsvReader;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class DemoSSHExample {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String ftpURL = "<<Host:IP>>";
String username = "<<Username>>";
String password = "<<password>>";
int port = 22;
String sftpDownloadDir = "file/to/download";
String sftpUploadDir = "file/to/upload";
Session session = null;
        Channel channel = null;
        ChannelSftp downloadChannelSftp = null;
        ChannelSftp uploadChannelSftp = null;
   
try {
//create a object
JSch jsch = new JSch();
//creating a session object
session= jsch.getSession(username, ftpURL, port);
//setting a password
session.setPassword(password);
// Create a Properties object of Java Util
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
//connecting to SFTP server
session.connect();
System.out.println("Host connected");
//creating a channel object for SFTP
            channel = session.openChannel("sftp");
            //connecting channel to SFTP Server
            channel.connect();
            System.out.println("sftp channel opened and connected.");
            // Type Caste a Channel object to ChannelSftp Object
            downloadChannelSftp = (ChannelSftp) channel;
            // Go to particular directory using SftpChannel
            downloadChannelSftp.cd(downloadChannelSftp.getHome() + "/" + sftpDownloadDir);
            //Print the current path on console
            System.out.println("Path = " + downloadChannelSftp.pwd());
         
            //getting a file from  SFTP Server to local system
            String fileName = "<<filename>>";
            File downloadFile = new File(downloadChannelSftp.getHome() + "/" + sftpDownloadDir + "/" + fileName);
            InputStream inputStream = downloadChannelSftp.get(downloadFile.getName());
           
            System.out.println("File Download successfully");
         
            // Upload a file to SFTP Server
           
            uploadChannelSftp = (ChannelSftp) channel;
            uploadChannelSftp.cd(uploadChannelSftp.getHome()+ "/" +sftpUploadDir);
         
            File uploadFile = new File(fileName);
            uploadChannelSftp.put(new FileInputStream(uploadFile),uploadFile.getName());
            System.out.println("File uploaded successfully");
         
} catch (JSchException exc) {
exc.printStackTrace();
} catch (SftpException exc) {
exc.printStackTrace();
} catch (FileNotFoundException exc) {
exc.printStackTrace();
}finally {
downloadChannelSftp.exit();
uploadChannelSftp.exit();
channel.disconnect();
session.disconnect();
}



}

}





Congratulation ! It's Done.




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.