user1078760
user1078760

Reputation: 749

How to download file via FTP using multithreading?

I have following task: to make Android program for downloading 1,000 files from FTP server (300 megabytes). Now I have made a program which parses tree of files and download it into SDCard. But it will take much time (40 minutes), and it isn't good, I need to reduce time of downloading. File is being downloaded with code:

BufferedOutputStream buffIn=new BufferedOutputStream(new FileOutputStream(f));
mClient.retrieveFile(ftpFile.getName(), buffIn);
buffIn.close();

But how can I use multithreading for it? I use apache-commons library, FTPClient class. I don't think that I should to copy code of downloading into Thread and it helps me. Please, help me, how can I reduce time of downloading?

Upvotes: 1

Views: 3763

Answers (1)

Telmo Marques
Telmo Marques

Reputation: 5106

Making multiple connections to download files from an FTP server may only be beneficial if that server limits the bandwidth for each connection, like expressed by @fge in the comments.

Let's take a look at some examples:

Suppose you have an 8 mbps connection to the Internet, which gives you a theoretical maximum download speed of 1 megabyte/sec.

Scenario 1: You're downloading a 100 megabyte file from an FTP server that doesn't impose a download limit.

If you're downloading the file using one connection, you download it at 1 megabyte/sec, what takes you 100 seconds (1min 40sec).

By using two connections (assuming it is possible over FTP to download different pieces of one file at the same time) you download the file at 0.5 megabytes/sec for each connection, totaling 1 megabyte/sec, what also takes you 100 seconds (1min 40sec) to download the file.

So we conclude that, in this scenario, multiple connections do not help.


Scenario 2: You're downloading a 100 megabyte file from an FTP server that imposes a download limit of 0.5 megabytes/sec for each connection.

If you're downloading the file using one connection, you download it at 0.5 megabytes/sec (imposed by the server), what takes you 200 seconds (3min 20sec).

By using two connections (assuming it is possible over FTP to download different pieces of one file at the same time) you download the file at 0.5 megabytes/sec for each connection, totaling 1 megabyte/sec, taking you 100 seconds (1min 40sec) to download the file.

So we conclude that, in this scenario, multiple connections do actually help.


Scenario 3: You're downloading multiple files - let's say two files of 100 megabytes each - from an FTP server (download limit disregarded as we've already seen that when a limit is imposed, multiple connections can help).

If you're downloading both files at the same time using one connection per file (two connections), you download each of the files at 0.5 megabytes/sec, what takes you 200 seconds (3min 20sec) to download both files. In other words, you only get the two files after 200 seconds.

On the other hand, downloading the files one by one, having only one connection at a time, you download both files at 1 megabyte/sec, what also takes 200 seconds (3min 20sec), but you get the first file after only 100 seconds (1min 20sec), and the seconds one 100 seconds later.

So we conclude that, in this scenario, it depends on whether the files are useful by themselves, or only as a whole.

Sorry for not being able to directly address your question, but I think you should first contemplate this scenarios and understand if there's something to get out of multiple connections, in your case.

Upvotes: 3

Related Questions