FTP apache commons progress bar in java
10,563
You can do it using the CopyStreamListener, that according to Apache commons docs is the listener to be used when performing store/retrieve operations.
CopyStreamAdapter streamListener = new CopyStreamAdapter() {
@Override
public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) {
//this method will be called everytime some bytes are transferred
int percent = (int)(totalBytesTransferred*100/yourFile.length());
// update your progress bar with this percentage
}
});
ftp.setCopyStreamListener(streamListener);
Hope this helps
Author by
cuzyoo
Updated on June 11, 2022Comments
-
cuzyoo almost 2 years
I'm working on a little program, which can upload a file to my FTP Server and do some other stuff with it. Now... It all works, i'm using the
org.apache.commons.net.ftp FTPClient
class for uploading.ftp = new FTPClient(); ftp.connect(hostname); ftp.login(username, password); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.changeWorkingDirectory("/shares/public"); int reply = ftp.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { addLog("Uploading..."); } else { addLog("Failed connection to the server!"); } File f1 = new File(location); in = new FileInputStream( ftp.storeFile(jTextField1.getText(), in); addLog("Done"); ftp.logout(); ftp.disconnect();
The file which should uploaded, is named in hTextField1. Now... How do i add a progress bar? I mean, there is no stream in ftp.storeFile... How do i handle this?
Thanks for any help! :)
Greetings
-
cuzyoo about 11 yearsOh, thank you so much, that works! But now i got another problem... If i press on the button which uploads the file, the program freezes, and if the upload is done the full progressbar is full...
-
BackSlash about 11 yearsThat's because you do the upload in the GUI thread, so the GUI freezes, and waits for your upload to be done, a way to avoid that is using Threads, there is an example: Thread Example
-
Michael Sanchez almost 11 yearsAfter hours of searching, finally found a solution that worked for me. +1! Thanks @BackSlash!