How do I upload file to FTP server?

This example demonstrate how to upload file to FTP server.

package org.kodejava.example.commons.net;

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

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

public class FileUploadDemo {
    public static void main(String[] args) {
        FTPClient client = new FTPClient();

        String filename = "touch.dat";
        try (FileInputStream fis = new FileInputStream(filename)) {
            client.connect("ftp.example.org");
            client.login("admin", "admin123**");

            // Store file to server
            client.storeFile(filename, fis);
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.4</version>
</dependency>

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.

8 Comments

  1. Hi,

    I am trying to upload a file of 3MB to FTP server using that code, but when I run it, it uploads only 40KB of the file. What to do?

    Thanks,
    Eden

    Reply
    • Hi Eden,

      Did you get any error message when uploading the file? You can try to call the client.getReplyCode() and client.getReplyString() to see if any error occurred while uploading. Just print the reply code and reply string message using System.out.println() after the client.storeFile() line.

      Reply
  2. I am getting below exception can you pls help me. Thanks in advance

    java.net.ConnectException: Connection timed out: connect
        at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:762)
        at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:565)
        at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:557)
        at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1795)
        at ftp.UploadFile.main(UploadFile.java:33)
    
    Reply
    • If you use another FTP client program to access the FTP server, can you connect to the FTP server? If you can connect, could it be an http proxy related issue?

      You could try use the FTPHTTPClient class instead of FTPClient. Using the FTPHTTPClient allows you to configure a proxy to connect to the FTP server. Do something like:

      FTPHTTPClient client = 
              new FTPHTTPClient("proxy.example.org", 8080, "username", "password");
      
      Reply
  3. org.apache.commons.net.ftp.FTPConnectionClosedException: FTP response 421 received.  Server closed connection.
        at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:360)
        at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
        at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:474)
        at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:547)
        at org.apache.commons.net.ftp.FTP.user(FTP.java:693)
        at org.apache.commons.net.ftp.FTPClient.login(FTPClient.java:872)
        at javaapplication1.UploadFile.main(UploadFile.java:26)
    

    Can’t upload my file to my server, please help me Sir.

    Reply
    • Error 421 Service not available, closing control connection. Error 421 User limit reached Error 421 You are not authorized to make the connection Error 421 Max connections reached Error 421 Max connections exceeded.

      One of these errors could be related to your problem. Have you try to restart your FTP service and retry the process?

      Reply
  4. Hi – I will have to copy xml file from local machine to both Windows and Linux servers through soapui. Can you please let me know through sopaui? Is there any setting that we can enable to proceed with this?

    Reply

Leave a Reply