This example demonstrates how to upload file to FTP server.
package org.kodejava.commons.net;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.io.InputStream;
public class FTPUploadDemo {
public static void main(String[] args) {
FTPClient client = new FTPClient();
String filename = "data.txt";
// Read the file from the resources folder.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try (InputStream is = classLoader.getResourceAsStream(filename)) {
client.connect("ftp.example.com");
boolean login = client.login("demo", "password");
if (login) {
System.out.println("Login success...");
// Store file to server
client.storeFile(filename, is);
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.10.0</version>
</dependency>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
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
Hi Eden,
Did you get any error message when uploading the file? You can try to call the
client.getReplyCode()
andclient.getReplyString()
to see if any error occurred while uploading. Just print the reply code and reply string message usingSystem.out.println()
after theclient.storeFile()
line.How to upload from a sd card from an Android device?
I am getting below exception can you pls help me. Thanks in advance
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 ofFTPClient
. Using theFTPHTTPClient
allows you to configure a proxy to connect to the FTP server. Do something like:Can’t upload my file to my server, please help me Sir.
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?
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?
Sorry I don’t have money, but thank you <3
Unfortunately, I can’t upload the file this way.
Hi Maciekk,
The code snippet above read the file from
resources
directory. In a maven project this will besrc/resources/data.txt
. If you want to read the file from an absolute path you can modify the code to be like:Thank you.
Can you write what it would look like in Kotlin?
I’m just getting started with the Kotlin language. I can’t cope with converting your code to Kotlin. Can you help?
I was able to login but I cannot upload the file.
I copied the file to
resources
and success. 🙂This is what the current code looks like.
Hi Maciekk,
Here is a simple version using Kotlin:
Thank you very much.
Thank you, I buy you coffee 🙂
Hi Maciekk,
You’re welcome. Thanks for your support.
Hi,
I entered the code in Android Studio. I cannot
import org.apache.commons.net.ftp.FTPClient
. I seecommons
in red. Intellij IDEA has approx.Hi,
Have you add the Apache Commons Net dependency in your module’s
build.gradle
file?Yes, it worked 🙂
Now I need to save the data to a file on the device and FTP 🙂
Is it possible to write variables directly to a file on FTP? I would like to skip operations on the local device because it is too difficult for me now.
Hi,
By writing variables I assume that you want to upload some string values to the FTP server.
If that’s correct, then you can convert the string into an
InputStream
and send it to the FTP server.Yes, I write some string e.g. (sum1 = 1, sum2 = 2 …)
Can you tell us how it will be written in Kotlin?
One more request. what it will look like the other way (reading).
In Kotlin?
I have
getBytes()
in red.I didn’t expect it to be so complicated to save a file to an Android device. https://youtu.be/WeV34UqhVvc
Hi,
You can do something like this in Kotlin.
or
Thank you.
I have one more question. I am trying to save a file (not ftp) on an Android device :-). I found materials but all of them use “context”. Whatever I do, context lights up red.
One code example I found on the web:
Uploading to ftp works, but when I put the code into android (HomeViewModel), the program cannot log into the ftp server. Please help. Coffee is on 🙂
HomeViewModel
Code:
Hi,
I guess you get an error like
android.os.NetworkOnMainThreadException
in your application. This exception thrown because you cannot perform networking operation on the main thread. You can use thejava.util.concurrent.ExecutorService
to execute it in a different thread.You can do something like this:
And also add to
AndroidManifest.xml
And how will it be to read the file from ftp?
Will you tell? 😉
Hi Maciekk,
You can check the following example to download file from FTP server: How do I download file from FTP server. But it is in Java language. You can leave your comment there if there are any questions.
Works 🙂 but …
the zapisStringFtp function is called each time Button is clicked. After the 4th click, the program (debug) throws an error: E/AndroidRuntime: FATAL EXCEPTION: pool-2-thread-4
Is ok, lines of code ran out:
Hi,
Good to know that it work!
This error FTP response 421 received. Server closed connection. can be cause by the server connection limit. Because we create a new connection on every upload without closing the previous one.