What is SFTP, simple java SFTP file upload code

codingbeginners_sftp
        Today i want to share SFTP (SSH File Transfer Protocol) concept. In county most sector will be change to paperless work and paperless system security is also important. So SSH File transfer protocol mostly used as compare to FTP (File Transfer Protocol). 
        

What is SFTP

SFTP stands for SSH File Transfer Protocol, or secure File Transfer Protocol, is a separate protocol packaged with SSH that works in a similar way over a secure connection.The advantage is a ability to leverage a secure connection to transfer lies and traverse the file system on both the local and remote system. SFTP is preferable to FTP because of its underlying security features and ability to piggy-back on an SSH connection. FTP is an insecure protocol that should be used limited cases or on networks you trust.

Features and Benefits

  •  Authentication mechanism :- 
These mechanism include username and password, SSH keys are access credentials that need to be part of identity. 
  •  Access control :- 
Server are accessible only to those with the right authorization.
  •  Data-at-motion encryption :- 
Data-in-motion encryption is usually provided through protocols like SSL or SSH.
  •  Antivirus  :- 
FTP server integrates with several antivirus software. At the time of file upload they  automatically start the virus scanning (like as Trojan and malware).

Java Example

Their are few steps as follows.
 

Step 1:

 
           Create simple java project using any java development tool like as eclipse, net-beans etc.  

Step 2:

 
           You need one jar files. That jar file name is  com.jcraft.jsch_0.1.27.jar 

If you can also download  jar file. Just click on below link
                                                     Download, jar file

Step 3:

 
           Create one package in that package create simple java class. This is a simple single java class example in that java class you just path local directory file location in main method.Copy below java code in your project class. 
 

SFTP_FileUploader.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.codingbeginners.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;

public class SFTP_FileUploader {

 public void fileUpload(String fileName) {
  String SFTPHOST = "xxx";
  int SFTPPORT = 44;
  String SFTPUSER = "abc";
  String SFTPPASS = "abc";
  String SFTPWORKINGDIR = "/x/x/CodingBeginners";

  Session session = null;
  Channel channel = null;
  ChannelSftp channelSftp = null;
  System.out.println("preparing the host information for sftp.");
  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();
   System.out.println("Host connected.");
   channel = session.openChannel("sftp");
   channel.connect();
   System.out.println("sftp channel opened and connected.");
   channelSftp = (ChannelSftp) channel;
   channelSftp.cd(SFTPWORKINGDIR);
   File f = new File(fileName);
   channelSftp.put(new FileInputStream(f), f.getName());
  } catch (Exception ex) {
   System.out.println("Exception found while tranfer the response.");
  } finally {

   channelSftp.exit();
   System.out.println("sftp Channel exited.");
   channel.disconnect();
   System.out.println("sftp Channel disconnected.");
   session.disconnect();
   System.out.println("Host Session disconnected.");
  }
 }

 public static void main(String[] args) {
  SFTP_FileUploader sftp = new SFTP_FileUploader();
  sftp.fileUpload("C:\Users\nilesh_vispute\Desktop\CodingBeginners_SFTP.wsdl");
 }
}

Step 4:

 
           Run main method class. Below screen shot indicate output.
 
 
 Server before uploading FTP file
 
 
See the below server screen screen. local directory file upload successfully on server
 
 
Using above steps you can easily upload file on server using SFTP.
 
Sample example file upload on server using SFTP are also provided. Just click on below link.

                          Download, sample java application (file upload using sftp)

 

        If you like this blog then please subscribe and suppose you have any queries or suggestion then comment below. 

 

One thought on “What is SFTP, simple java SFTP file upload code”

Leave a Reply

Your email address will not be published. Required fields are marked *