Search This Blog

Thursday, September 29, 2016

Upload a file to SFTP using java

package com.sftp;

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

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * @author drvijay
 */

public class SFTPcallInJava
{

public static void main ( String [] args )
{
String SFTPHOST = "192.168.7.99";
int SFTPPORT = 2121;
String SFTPUSER = "vijayjava";
String SFTPPASS = "vijaytest123";
String SFTPWORKINGDIR = "/www/";

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

try
{
JSch jsch = new JSch ();
session = jsch.getSession ( SFTPUSER, SFTPHOST, SFTPPORT );
session.setPassword ( SFTPPASS );
java.util.Properties config = new java.util.Properties ();
config.put ( "StrictHostKeyChecking", "no" );
session.setConfig ( config );
session.connect ();
channel = session.openChannel ( "sftp" );
channel.connect ();
channelSftp = (ChannelSftp) channel;
channelSftp.cd ( SFTPWORKINGDIR );
File f = new File ( "D:/Temp/aamlog.txt" );
channelSftp.put ( new FileInputStream ( f ), f.getName () );
}
catch ( Exception ex )
{
ex.printStackTrace ();
}

}

}



NOTE - Add jsch-0.1.54.jar

No comments:

Hit Counter


View My Stats