简体   繁体   English

如何使用多线程通过FTP下载文件?

[英]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). 我有以下任务:制作Android程序,从FTP服务器下载1,000个文件(300兆字节)。 Now I have made a program which parses tree of files and download it into SDCard. 现在我已经制作了一个解析文件树并将其下载到SDCard的程序。 But it will take much time (40 minutes), and it isn't good, I need to reduce time of downloading. 但是这将花费很多时间(40分钟),而且不好,我需要减少下载时间。 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. 我使用apache-commons库,FTPClient类。 I don't think that I should to copy code of downloading into Thread and it helps me. 我不认为我应该将下载代码复制到Thread中,它对我有所帮助。 Please, help me, how can I reduce time of downloading? 请帮助我,如何减少下载时间?

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. 如果服务器限制每个连接的带宽(如注释中的@fge所表示),则建立多个连接以从FTP服务器下载文件可能是有益的。

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. 假设您有一个8 mbps的Internet连接,这使您的理论最大下载速度为1兆字节/秒。

Scenario 1 : You're downloading a 100 megabyte file from an FTP server that doesn't impose a download limit. 方案1 :您正在从未强加下载限制的FTP服务器下载100兆字节的文件。

If you're downloading the file using one connection, you download it at 1 megabyte/sec, what takes you 100 seconds (1min 40sec). 如果您使用一个连接下载文件,则以1兆字节/秒的速度下载,需要100秒(1分40秒)。

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. 通过使用两个连接(假设可以通过FTP同时下载一个文件的不同部分),每个连接以0.5兆字节/秒的速度下载文件,总计1兆字节/秒,还需要100秒(1分钟) 40秒)下载文件。

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. 场景2 :您正在从FTP服务器下载100兆字节的文件,该文件对每个连接施加0.5兆字节/秒的下载限制。

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). 如果您使用一个连接下载文件,则以0.5兆字节/秒(由服务器强加)下载文件,需要200秒(3分20秒)。

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. 通过使用两个连接(假设可以通过FTP同时下载一个文件的不同部分),每个连接以0.5兆字节/秒的速度下载文件,总计1兆字节/秒,花费100秒(1分40秒)下载文件。

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). 场景3 :你正在下载多个文件 - 比如两个100兆字节的文件 - 来自FTP服务器(下载限制被忽略,因为我们已经看到,当施加限制时,多个连接可以提供帮助)。

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. 如果您使用每个文件一个连接(两个连接) 同时下载两个文件,则以0.5兆字节/秒下载每个文件,下载这两个文件需要200秒(3分20秒)。 In other words, you only get the two files after 200 seconds. 换句话说,您只能在200秒后获得两个文件。

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. 另一方面,一次只连接一个文件,一次又一次地下载文件,您以1兆字节/秒的速度下载两个文件,这也需要200秒(3分20秒), 但是只有100秒后您才能获得第一个文件(1分20秒),100秒后的秒数。

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. 很抱歉无法直接解决您的问题,但我认为您应首先考虑这些方案,并了解在您的情况下是否有什么东西可以摆脱多个连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM