Connect to SFTP Server Example
This program demonstrates connecting to an SFTP server.
Problem Statement
Write a Java program that connects to an SFTP server using JSch library.
Source Code
| 1 | import com.jcraft.jsch.*; |
| 2 | public class SFTPConnectExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq SFTP Connect Example"); |
| 5 | JSch jsch=new JSch(); |
| 6 | try { |
| 7 | Session session=jsch.getSession("user","sftp.eduinq.com",22); |
| 8 | session.setPassword("password"); |
| 9 | session.setConfig("StrictHostKeyChecking","no"); |
| 10 | session.connect(); |
| 11 | System.out.println("Eduinq SFTP Connected Successfully"); |
| 12 | session.disconnect(); |
| 13 | } catch(Exception e) { |
| 14 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 15 | } |
| 16 | } |
| 17 | } |
Program Output
Eduinq SFTP Connect Example Eduinq SFTP Connected Successfully
Explanation
Uses JSch Session for secure connection. - Port 22 used for SFTP. - Exception handling ensures safe operations. - In enterprise systems, SFTP secures file transfer. Conclusion: Shows connecting to SFTP server.