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.
package org.kodejava.commons.net;
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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.connect("ftp.example.com");
boolean login = client.login("demo", "password");
if (login) {
System.out.println("Login success...");
// Download file from FTP server.
boolean status = client.retrieveFile(filename, os);
System.out.println("status = " + status);
System.out.println("reply = " + client.getReplyString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
If the download process was success you will see the following output printed:
status = true
reply = 226 Transfer complete.
Maven Dependencies
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.9.0</version>
</dependency>
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023
But how can I choose the path that the file will be downloaded at?
The download path can be defined in the
FileOutputStream
object. You can pass a string with a full path or you can createFile
object that define the full path where the download file will be stored.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 mymainActivity
like this:Can you help me?
Hi Mona,
Can you show me the full code on how you integrate the code in the example above in your Android app? Maybe you can post the code in Pastebin.com.
java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClient
You need the Apache Commons Net library to compile and run the example.
I’m downloading File to a specific directory, but the file comes empty.
Hi Victor,
Try to get the download status and the reply code from the FTP server by adding the following code. This will show if the download process success or failed.
Hi, I have the following reply “425 Could not open data connection to port 52331: Connection timed out”. How can I fix it?
I’ve been trying different ways for several days and got stuck. I log in to FTP, but I cannot download data from the file. I don’t want to save them locally, I just want to download them, put them into an array, count them and save them again on FTP or just display or print them.
Here are my attempts with the code:
Preferably, if it was possible to download data from the file directly from ftp, without saving (android creates problems with saving files on the device).
Hi Maciekk,
What about the following snippet? We convert the output stream into string and then process it.
Thank you very much again!
You are my best Kotlin and Android teacher. 🙂
Of course the coffee is running 🙂
I was able to write the numbers from the txt file to the array, but I can’t get the quantity to determine the size of the array.
I did it but a bit weird 🙂
However, wrong. I can’t write these numbers from a file to an array. the program prints all numbers (number)
77
16
33
…
but I can’t put them in the board.
I try different ways:
The problem is with “77
and last “.
I don’t know how to get rid of “”.
How do I download file in browser using above program @Wayan Saryada
Hi Rama,
It’s depend on what framework you are using. If you are using a simple servlet, this example might give you a pointer: https://kodejava.org/how-do-i-create-zip-file-in-servlet-for-download/.