Pages Navigation Menu

Coding is much easier than you think

Uploading file on FTP Server

Uploading file on FTP Server


This tutorial contains description of file uploading to the FTP server using java.

File Upload :

FTPClient class supplies method to upload files from local/remote computer to the FTP Server. We need another class FileInputStream to handle the file during the transfer.
** UPDATE: FTP Complete tutorial now available here.

  • boolean storeFile(String remoteFile, InputStream local) : This method stores file on the server with the specified name and take input from the InputStream.
  • OutputStream storeFileStream(String remoteFile) : Its return type is OutputStream. It represents which data can be written to store file on the FTP server using specified name.
  • boolean storeUniqueFile(InputStream local) : This method takes input from the specified InputStream and stores file on the server with unique name assigned by the server.
  • boolean storeUniqueFile(String remoteFile, InputStream local) : Takes input from the given InputStream and stores a file on the server using a unique name derived from the specified name.
  • OutputStream storeUniqueFileStream() : Return type of this method is OutputStream which is used to write data to store file on the server with unique name assigned by the server.
  • OutputStream storeUniqueFileStream(String remoteFile) : This method returns OutputStream, which is used to write data to store file on the server with unique name derived from the given name.

Here we are using storeFile(String remoteFile, InputStream local) method. It throws FTPConnectionClosedException, CopyStreamException and IOException.

Example :

This example upload “File.doc” to the FTP server. client.storeFile(testName,fis); returns true if file uploaded successfully otherwise return false

File : FtpFileUpload.java

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

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

class FTPFileUpload {
	public static void main(String[] args) throws IOException {
		FTPClient ftpclient = new FTPClient();
		FileInputStream fis = null;
		boolean result;
		String ftpServerAddress = "localhost";
		String userName = "admin";
		String password = "admin";

		try {
			result = ftpclient.login(userName, password);

			if (result == true) {
				System.out.println("Logged in Successfully !");
			} else {
				System.out.println("Login Fail!");


			File file = new File("D:/File.doc");
			String testName = file.getName();
			fis = new FileInputStream(file);

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

			if (result == true) {
				System.out.println("File is uploaded successfully");
			} else {
				System.out.println("File uploading failed");
		} catch (FTPConnectionClosedException e) {
		} finally {
			try {
			} catch (FTPConnectionClosedException e) {

About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: