I am using apache commons-net for downloading a file from an FTP server. That much is working fine. The part I am having a problem with is showing the download progress using a JProgressBar.
The following code demonstrates how I am downloading the file I need:
public void download() {
try {
FTPClient ftpClient = new FTPClient();
String fileName = "OFMEX_MANUFACTURING.jar";
ftpClient.connect("192.168.1.242");
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
JOptionPane.showMessageDialog(null, "Server Down");
}
boolean login = ftpClient.login("bioftp", "bioftp");
boolean changeWorkingDirectory = ftpClient.changeWorkingDirectory("ofmex\\Linux\\");
boolean setFileType = ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
OutputStream data = (OutputStream) new FileOutputStream(fileName);
ftpClient.retrieveFile(fileName, data);
ftpClient.abort();
} catch (Exception e) {
e.printStackTrace();
}
}
If You are going to call
retriveFile()directly then you may not be able to know how many bytes are transferred. Instead, you can retrieve a file byte by byte and keep count of every byte transferred. You can also get the total bytes to be transferred so that you can calculate a percentage to show in the progress bar.See this apache commons util used in above code.