Home Java Javaftp FTP Server : Download file

Related Tutorials


Share on Google+Share on Google+

FTP Server : Download file

This tutorial contains description of file downloading from the FTP server using java.

FTP Server : Download file

This tutorial contains description of file downloading from the FTP server using java.

Download File : FTPClient class supplies method to download  files from FTP Server computer to the local/remote. We need another class FileOutputStream to handle the file during the transfer.

  • boolean retrieveFile(String remoteFile, OutputStream local) : This method fetches a specified file from the server and writes it to the specified OutputStream.
  • InputStream retrieveFileStream(String remoteFile) : This method returns an InputStream which is used to read the specified file from the server.

Here we are using retrieveFile(String remoteFile, OutputStream local) method. It throws FTPConnectionClosedException, CopyStreamException and IOException. This method is of boolean type and returns true if successfully downloaded otherwise returns false.

Example :

import java.io.FileOutputStream;
import java.io.IOException;

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

class FtpDownloadFile {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		FileOutputStream fos = null;
		boolean result;
		try {
			result = client.login("admin", "admin");

			if (result == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
			String fileName = "test.txt";
			fos = new FileOutputStream(fileName);

			// Download file from the ftp server
			result = client.retrieveFile(fileName, fos);

			if (result == true) {
				System.out.println("File is downloaded successfully!");
			} else {
				System.out.println("File downloading failed!");
		} catch (FTPConnectionClosedException e) {
		} finally {
			try {
				if (fos != null) {
			} catch (FTPConnectionClosedException e) {

Output :

Successfully logged in!
File is downloaded successfully!

If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 8, 2012

Related Tutorials

Discuss: FTP Server : Download file  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image