Home Java Javaftp FTPClient : Server File transfer

Related Tutorials


Share on Google+Share on Google+

FTPClient : Server File transfer

In this tutorial, you will learn how to transfer file on the server by using FTPClient class.

FTPClient : Server File transfer

In this tutorial, you will learn how to transfer file on the server by using FTPClient class.

Server File Transfer :

You can transfer file from your local to ftp server and also download files from the server. FTPClient class provides you many methods for uploading and downloading files to the server.

Uploading File :

public boolean storeFile(String remoteFile, InputStream local): This method of FTPClient class is used for storing the file on the server. Server takes input from the InputStream and uses the specified name. It is of boolean type and returns true if file is successfully uploaded on the server otherwise returns false. It throws FTPConnectionClosedException, CopyStreamException and IOException.

Other methods of file uploading are - OutputStream storeFileStream(String remoteFile), boolean storeUniqueFile(InputStream local), boolean storeUniqueFile(String remoteFile, InputStream local), OutputStream storeUniqueFileStream(), OutputStream storeUniqueFileStream(String remoteFile)

Downloading File :

boolean retrieveFile(String remote,OutputStream local) : This method returns boolean value. True value shows that the file is downloaded successfully otherwise it returns false. It fetches the file from the server and stores into the specified OutputStream. It throws FTPConnectionClosedException, CopyStreamException and IOException.

Other method of file downloading is - InputStream retrieveFileStream(String remoteFile).

Example : This example represents how file is transferred from local to the ftp server.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

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

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

			if (result == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
			File file = new File("C:/FtpTest.txt");
			String testName = file.getName();
			fis = new FileInputStream(file);

			// Upload file to the ftp server
			result = client.storeFile(testName, fis);

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

Output :

Successfully logged in!
File is transferred 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: FTPClient : Server File transfer  

Post your Comment

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