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 = "127.0.0.1";
        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);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

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

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

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

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

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ftpClient.logout();
                ftpClient.disconnect();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

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

Leave a Comment