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 :-
- Access control :-
- Data-at-motion encryption :-
- Antivirus :-
Java Example
Step 1:
Step 2:
If you can also download jar file. Just click on below link.
Download, jar file
Step 3:
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:
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.
Hello Sir,
This java code working fine for me.
Thanks…