Java SFTP Connection Files Transfer

Java SFTP Connection Files Transfer

In this article, I am going to show SFTP connection with java, transfer files from the local system to the remote system. Here we are going to using the JSch library to connect SFTP/STP server.

Pre-Requisites

  • Java 1.8
  • JSch.jar
  • Eclipse IDE
  • Maven

 

SftpChannel.java

We need to get SFTP server details as follows

  • SFTP Host/IP
  • SFTP Port
  • SFTP Username
  • SFTP Password
jsch = new JSch();
session = jsch.getSession(USER, HOST, Integer.parseInt(PORT));
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(PASSWORD);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
sftpchannel = (ChannelSftp) channel;

 

Complete Code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.narayanatutorial.sftp;

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

/**
 *
 * @author narayanatutorial
 */
public class SftpChannel {

    public static void main(String args[]) {
        Session session = null;
        ChannelSftp sftpchannel = null;
        Channel channel = null;
        JSch jsch = null;
        String USER="TESTUSER";
        String HOST="127.0.0.1";
        String PORT="1253";
        String PASSWORD="TESTPASSWORD";
        String SourceFile="D:/sample.txt";
        String DestinationFile="/home/user/sample.txt";
        String DestinationPath="/home/user/sampledirectory/";
        try {
            jsch = new JSch();
            session = jsch.getSession(USER, HOST, Integer.parseInt(PORT));
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(PASSWORD);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            sftpchannel = (ChannelSftp) channel;
            if (sftpchannel.isConnected()) {
                System.out.println("SFTP Connected");
            } else {
                System.out.println("SFTP Not Connected");
            }
            //transfer file from local to remote
            sftpchannel.put(SourceFile, DestinationFile);
            //create folder in the remote location
            sftpchannel.mkdir(DestinationPath);


        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if (session.isConnected()) {
                    session.disconnect();
                }
                if (channel.isConnected()) {
                    channel.disconnect();
                }
                if (!sftpchannel.isConnected()) {
                    sftpchannel.disconnect();
                }
        }

    }
}

 

pom.xml

<dependency>
  <groupId>com.jcraft</groupId>
  <artifactId>jsch</artifactId>
  <version>0.1.55</version>
</dependency>

 

Complete pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.narayanatutorial.sftp</groupId>
  <artifactId>Java_SFTP</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Java_SFTP</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

 

Get complete source code from Github

 

 

Leave a Reply