With the help of Apache Commons Net API, it is easy to write Java code for downloading a file from a remote FTP server to local computer. In this article, you will learn how to properly implement Java code to get files downloaded from a server via FTP protocol. A working sample program also provided.

Table of Content:

    1. Apache Commons Net API for downloading files by FTP protocol
    2. The proper steps to download a file
    3. Sample program code
    4. Compile and run the sample program
 

1. Apache Commons Net API for downloading files by FTP protocol

 The org.apache.commons.net.ftp.FTPClientclass provides two methods for downloading files from a FTP server:

Which method is used suitable for you? Here are few tips:

In addition, the following two methods must be invoked before calling the retrieveFile() and retrieveFileStream() methods:

 

2. The proper steps to download a file

 Here are the steps to properly implement code for downloading a remote file from a FTP server using Apache Commons Net API which is discussed so far:

 

3. Java FTP File Download Sample program code



In the following sample program, using both methods is implemented for transferring a file from the FTP server to local computer. Here is the program’s source code:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

/**
 * A program demonstrates how to upload files from local computer to a remote
 * FTP server using Apache Commons Net API.
 * @author www.codejava.net
 */
public class FTPDownloadFileDemo {

	public static void main(String[] args) {
		String server = "www.myserver.com";
		int port = 21;
		String user = "user";
		String pass = "pass";

		FTPClient ftpClient = new FTPClient();
		try {

			ftpClient.connect(server, port);
			ftpClient.login(user, pass);
			ftpClient.enterLocalPassiveMode();
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

			// APPROACH #1: using retrieveFile(String, OutputStream)
			String remoteFile1 = "/test/video.mp4";
			File downloadFile1 = new File("D:/Downloads/video.mp4");
			OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
			boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
			outputStream1.close();

			if (success) {
				System.out.println("File #1 has been downloaded successfully.");
			}

			// APPROACH #2: using InputStream retrieveFileStream(String)
	        String remoteFile2 = "/test/song.mp3";
	        File downloadFile2 = new File("D:/Downloads/song.mp3");
	        OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
	        InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2);
	        byte[] bytesArray = new byte[4096];
	        int bytesRead = -1;
	        while ((bytesRead = inputStream.read(bytesArray)) != -1) {
	        	outputStream2.write(bytesArray, 0, bytesRead);
	        }

	        success = ftpClient.completePendingCommand();
			if (success) {
				System.out.println("File #2 has been downloaded successfully.");
			}
			outputStream2.close();
			inputStream.close();

		} catch (IOException ex) {
			System.out.println("Error: " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			try {
				if (ftpClient.isConnected()) {
					ftpClient.logout();
					ftpClient.disconnect();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}
 

4. Compile and run the sample program

To compile and run the program, you need an Apache Commons Net’s jar library file presented in the classpath. Click here to download the latest distribution of Apache Commons Net.

Extract the distribution zip file and copy the commons-net-VERSION.jar file into the same folder of the FTPDownloadFileDemo.java file.

Type the following command to compile the program:

javac –cp commons-net-VERSION.jar FTPDownloadFileDemo.java

And type this command to run the program:

java –cp commons-net-VERSION.jar;. FTPDownloadFileDemo

 

 

NOTE: You need to replace VERSION by the actual version number of the jar file. At the time of writing this article, the latest version of Apache Commons Net API is 3.6, hence commons-net-3.6.jar

 

Related Java FTP File Download Tutorials:

 

Other Java File Download Tutorials:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (FTPDownloadFileDemo.java)FTPDownloadFileDemo.java[Java FTP File Download Program]2 kB