Download File from FTP Example

This program demonstrates downloading a file from an FTP server.

Problem Statement

Write a Java program that downloads a file from an FTP server using Apache Commons Net.

Source Code

1 import java.io.*;
2 import org.apache.commons.net.ftp.FTPClient;
3 public class FTPDownloadExample {
4 public static void main(String[] args) {
5 System.out.println("Eduinq FTP Download Example");
6 FTPClient ftp=new FTPClient();
7 try {
8 ftp.connect("ftp.eduinq.com");
9 ftp.login("user","password");
10 FileOutputStream fos=new FileOutputStream("downloaded.txt");
11 boolean done=ftp.retrieveFile("server/sample.txt",fos);
12 fos.close();
13 System.out.println("Eduinq Download Success: "+done);
14 ftp.logout();
15 ftp.disconnect();
16 } catch(Exception e) {
17 System.out.println("Eduinq Exception: "+e.getMessage());
18 }
19 }
20 }

Program Output

					Eduinq FTP Download Example
Eduinq Download Success: true
				

Explanation

Uses retrieveFile() to download file. - Saves file locally. - Exception handling ensures safe operations. - In enterprise systems, FTP download retrieves shared data. Conclusion: Shows downloading files from FTP server.

Quick Links to Explore