How do I download file from FTP server?

This example demonstrates how to download a file from FTP server. To download a file we first connect to the FTP server and then login by supplying the username and password. To download the file we call retrieveFile() method of the FTPClient object. This method takes two parameters, the remote filename and an OutputStream of the local file where the download to be saved.




public class FTPDownloadDemo {
    public static void main(String[] args) {
        // The local filename and remote filename to be downloaded.
        String filename = "data.txt";

        FTPClient client = new FTPClient();
        try (OutputStream os = new FileOutputStream(filename)) {
            client.login("admin", "admin123**");

            // Download file from FTP server.
            client.retrieveFile("/" + filename, os);
        } catch (IOException e) {
        } finally {
            try {
            } catch (IOException e) {

Maven Dependencies

<!-- -->

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.


  1. The download path can be defined in the FileOutputStream object. You can pass a string with a full path or you can create File object that define the full path where the download file will be stored.

  2. I tried the code but my app has stopped 🙁 The changes that I made with the code are: with the server name and login user as well as the directory for the file in the server.

    When i call this class in the mainActivity what should I send? I call it in my mainActivity like this:

    startActivity(new Intent(MainActivity.this, FTPDownloadFileDemo.class));

    Can you help me?

Leave a Reply