Home Java Javaftp FTP File Upload in Java


Share on Google+Share on Google+

FTP File Upload in Java

Learn how to write code for FTP File Upload in Java.

FTP File Upload in Java

This tutorial shows you how you can write Java program that uploads the file on FTP server. Using Apache FTPClient it's very easy to write code for FTP File Upload in Java. I am assuming that you have FTP server ready and you have credentials to connect to FTP server. Your FTP account must have read/write write on the server. After completing this example you will be able to create programs in Java that uploads the file on FTP server.

We will use FTPClient utility class from Apache commons library. The org.apache.commons.net.ftp.FTPClient class provides necessary methods necessary to make a connection to FTP Server and perform upload/download/move/delete operations. The apache commons library also provides an exception class org.apache.commons.net.ftp.FTPConnectionClosedException for handling exception.

We have a FTP server installed on our local machine, which we will use for testing our FTP File Upload in developed Java. You can just change the IP address, username and password and then test the example program at your end.

Step 1:

Download the apache commons library and include commons-net-3.2.jar in your class path. Alternatively you can include the jar (commons-net-3.2.jar) file into your project.

Step 2:

Write the code that connects to FTP server and uploads the file. Here is the complete code of the program:

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

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

class FTPFileUploadInJava {
        public static void main(String[] args) throws IOException {
                FTPClient client = new FTPClient();
                FileInputStream fis = null;
                boolean result;
				String ftpServerAddress="";
				String userName="deepak";
				String password="deepak";

                try {
                        result = client.login(userName, password);
                        if (result == true) {
                                System.out.println("Successfully logged in!");
                        } else {
                                System.out.println("Login Fail!");


                        File file = new File("d:/uploadtest.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");
                } catch (FTPConnectionClosedException e) {
                } finally {
                        try {
                        } catch (FTPConnectionClosedException e) {

Following credentials is used to connect to the FTP server:

String ftpServerAddress="";
String userName="deepak";
String password="deepak";

Code to login to the server is client.login(userName, password);

Code to upload the file is client.storeFile(testName, fis);

Read more at Java FTP Tutorials.


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 16, 2012

Related Tutorials

Ask Questions?    Discuss: FTP File Upload in Java  

Post your Comment

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