Download File from SFTP Example
This program demonstrates downloading a file from an SFTP server.
Problem Statement
Write a Java program that downloads a file from an SFTP server using JSch library.
Source Code
| 1 | import com.jcraft.jsch.*; |
| 2 | import java.io.*; |
| 3 | public class SFTPDownloadExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq SFTP Download 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.get("/server/sample.txt","downloaded.txt"); |
| 16 | System.out.println("Eduinq File Downloaded 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 Download Example Eduinq File Downloaded Successfully
Explanation
Uses ChannelSftp.get() to download file. - Saves file locally. - Exception handling ensures safe operations. - In enterprise systems, SFTP download retrieves secure data. Conclusion: Shows downloading files from SFTP server.