Upload File to SFTP Example
This program demonstrates uploading a file to an SFTP server.
Problem Statement
Write a Java program that uploads a file to an SFTP server using JSch library.
Source Code
| 1 | import com.jcraft.jsch.*; |
| 2 | import java.io.*; |
| 3 | public class SFTPUploadExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq SFTP Upload Example"); |
| 6 | JSch jsch=new JSch(); |
| 7 | try { |
| 8 | Session session=jsch.getSession("user","sftp.eduinq.com",22); |
| 9 | session.setPassword("password"); |
| 10 | session.setConfig("StrictHostKeyChecking","no"); |
| 11 | session.connect(); |
| 12 | Channel channel=session.openChannel("sftp"); |
| 13 | channel.connect(); |
| 14 | ChannelSftp sftp=(ChannelSftp)channel; |
| 15 | sftp.put("sample.txt","/upload/sample.txt"); |
| 16 | System.out.println("Eduinq File Uploaded Successfully"); |
| 17 | sftp.disconnect(); |
| 18 | session.disconnect(); |
| 19 | } catch(Exception e) { |
| 20 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 21 | } |
| 22 | } |
| 23 | } |
Program Output
Eduinq SFTP Upload Example Eduinq File Uploaded Successfully
Explanation
Uses ChannelSftp.put() to upload file. - Secure transfer over SSH. - Exception handling ensures safe operations. - In enterprise systems, SFTP upload secures sensitive data. Conclusion: Shows uploading files to SFTP server.