Pages Navigation Menu

Coding is much easier than you think

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 {
			ftpclient.connect(ftpServerAddress);
			result = ftpclient.login(userName, password);

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

			ftpclient.changeWorkingDirectory("/");

			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");
			}
			ftpclient.logout();
		} catch (FTPConnectionClosedException e) {
			e.printStackTrace();
		} finally {
			try {
				ftpclient.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}
	}
}

3 Comments

  1. Dear Friends,
    for each program input and output is important please add it

  2. Dear Sir,

    Your code is not working, I get error File uploading failed. what problem is that ?

  3. this is it