FTP Server : Upload file

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

FTP Server : Upload file

FTP Server : Upload file

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.

  •  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 "test.txt" to the FTP server. client.storeFile(testName, fis); returns true if file uploaded successfully otherwise return false.

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 FtpUploadFile {
	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:/test.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 uploaded successfully");
			} else {
				System.out.println("File uploading failed");
			}
			client.logout();
		} catch (FTPConnectionClosedException e) {
			e.printStackTrace();
		} finally {
			try {
				client.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}
	}
}

Output :

Successfully logged in!
File is uploaded successfully