FTP File Upload in Java

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

FTP File Upload in Java

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

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="192.168.10.17";
				String userName="deepak";
				String password="deepak";

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

						client.changeWorkingDirectory("/");


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

Following credentials is used to connect to the FTP server:

String ftpServerAddress="192.168.10.17";
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.