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

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 {
			client.connect("localhost");
			result = client.login("admin", "admin");

			if (result == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
				return;
			}
			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.");
			}
			client.logout();
		} catch (FTPConnectionClosedException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				client.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}
	}
}

Output :

Successfully logged in!
File is transferred successfully..