Upload file on ftp server using java

In this tutorial, we will see how to upload file on ftp server using java. To upload file on ftp sever using java, we can use the Apache Commons Net library.

Code to upload file on ftp server using java:-
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

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

public class FTPFileUpload {

    public static void main(String... args) {

        String server = "";
        int port = 21;
        String username = "your_username";
        String password = "your_password";
        String remoteFilePath = "/ftp";
        String localFilePath = "c:\\test.txt";

        FTPClient ftpClient = new FTPClient();
        ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));

        try {

            ftpClient.connect(server, port);
            ftpClient.login(username, password);

            if (!ftpClient.changeWorkingDirectory(remoteFilePath)) {
                System.out.println("Remote directory does not exist.");

            File localFile = new File(localFilePath);
            FileInputStream inputStream = new FileInputStream(localFile);

            boolean uploadFile = ftpClient.storeFile(localFile.getName(), inputStream);

            if (uploadFile) {
                System.out.println("File uploaded successfully.");
            } else {
                System.out.println("File upload failed.");

        } catch (IOException e) {
        } finally {
            try {
            } catch (IOException ex) {

The provided Java code is an example of how to use the Apache Commons Net library to upload a file to an FTP server. It demonstrates how to connect to an FTP server, log in, navigate to a specific directory on the server, and upload a file.

Let’s break down the code step by step:

1. An instance of FTPClient is created to manage the FTP connection.

2. A command listener is added to the FTPClient to print the FTP commands and server responses to the   console for debugging purposes.

3. ftpClient.enterLocalPassiveMode() enables passive mode for the FTP connection.

4. ftpClient.setFileType(FTP.BINARY_FILE_TYPE) sets the file transfer type to binary mode.

5. ftpClient.changeWorkingDirectory(remoteFilePath) checks if the remote directory specified by remoteFilePath exists on the server. If the directory does not exist, it prints a message and returns.

6. It creates a File object for the local file specified by localFilePath.

7. Opens an FileInputStream to read the local file.

8. Uploads the file to the FTP server using ftpClient.storeFile(localFile.getName(), inputStream). It stores the file with the same name as the local file.

9. After the upload is complete, it checks the return value of ftpClient.storeFile(). If true, it prints “File uploaded successfully.” If false, it prints “File upload failed.”

10. The code includes exception handling for potential IOExceptions that can occur during FTP operations.

11. In the finally block, the code logs out from the FTP server using ftpClient.logout() and disconnects from the server using ftpClient.disconnect().

This code provides a basic example of how to upload file on FTP server using Apache Commons Net.

You should replace the placeholder values for server, username, password, remoteFilePath, and localFilePath with your actual FTP server details and the file you want to upload.

Source Code:- Download source code

inner classes in java

in this post we will discuss about the inner classes in java. so what is inner class and when we should go for inner classes. Sometimes when we declare a class inside another class, such type of class is called inner class. class Car { System.out.println(“inside car class”); class Engine{ System.out.println(“inside engine inner class”); } … Read more

implement selection sort algorithm in java

In this tutorial we will see that how to implement selection sort algorithm in java. The selection sort is a combination of searching and sorting. During each pass, the unsorted element with the smallest or largest value is moved to its proper position in the array. The number of times the sort passes through the … Read more

User defined exception in java

User defined exception in java can be created by extending any predefined exception. User defined exception in java are also called Custom Exceptions. In java if we are developing an application and get some requirement to instruct the user who is going to use our class about the problem occurred while executing the statement. To … Read more

Create Table Dynamically using Jquery

In this tutorial we will create table dynamically using jquery 1.10. In the below example we are using jQuery.getJSON( url, [data] [success] ) function of jquery to load json encoded data from server using get http request. Example to Create Table Dynamically using Jquery:- <!doctype html> <html lang=”en”> <head> <meta charset=”utf-8″> <title>Create Table Dynamically using … Read more

AngularJS an Overview

AngularJS is a powerful JavaScript based framework to build large scale, high performance web application. It is a cross-browser compliant, open source and used for creating single page web applications. It is an open-source web application framework mainly maintained by Google and by a community of individuals. It aims to simplify both the development and … Read more

Connection Pooling in JDBC using Apache Commons DBCP

In this Example We will implement the connection pooling in JDBC using Apache Commons DBCP. Now what is Connection Pool? Connection Pool is a cache of database connections so that connection can be reused when future requests to the database are required. Opening a database connection for each user is a wastage of resource and … Read more

Get Date Using JavaScript

We can get Date using JavaScript built in object Date. In below example we will see how to get date using JavaScript Date object. example to get current date using JavaScript in dd-mm-yyyy format:- var today = new Date(); var day = today.getDate(); var month = today.getMonth()+1; //first month starts from 0 var year = … Read more

Enum Types in Java

Enum was introduced in java 5. Enum types in java is a special type of class to define collection of constants. Variables in enum are constants so there name should be in uppercase letters. In java we define an enum type by using the enum keyword. Enum types in java is used to represent a … Read more