如何打开多个连接来下载单个文件?(How to open multiple connections to download single file?)

我知道有些服务器不允许这样做,但有些服务器支持多个连接。 我可以下载部分文件,并在下载最后一部分后将它们合并,因为我为每个文件部分使用单独的后台工作器...所以速度很慢,并且一次下载一个文件部分。 我想要立即开始下载每个文件部分。 但我不知道该怎么做。 告诉我哪种方法更快,以及如何使用它们。

Backgroundworker Threads ThreadPool

谢谢你的帮助。

I know that some servers dose not allow this, but some does support multiple connections. I am able to download file in parts and combine them after downloading last part , as i am using separate background worker for each filepart...so it is slow and it downloads one file part at a time. I want to start downloading each file part at once. But i don't know how to do this. Tell me which method is more fast and how to use them.

Backgroundworker Threads ThreadPool

Thanks for your help.

最满意答案

如果您使用HTTP下载文件,则可以使用此方法:

http://msdn.microsoft.com/en-us/library/7fy67z6d.aspx

所以你必须将文件分割成多个临时文件,然后合并它们。

但是你必须确定在服务器端启用了这个功能(我不知道它是否默认)。

正如有人所说,你会获得高性能,这就是免费下载管理器如此有用的原因:它同时下载文件的多个部分。

用多线程做到这一点:

class FileDownloader{ int Start; int Count; string PathTemp; string Url; FileDownloader(url,start,count){ url = Url; Start =start; Count = count; PathTemp = Path.GetTempFileName() } void DoDownload(){ //do your thing with stream and request and save it to PathTemp } }

这里是你的代码来初始化你的下载器列表:

List<FileDownloader> filewonloadersList = new ListFileDownloader>(); System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://stackoverflow.com/robots.txt"); req.Method = "HEAD"; System.Net.WebResponse resp = req.GetResponse(); int responseLength = int.Parse(resp.Headers.Get("Content-Length")); for(int i = 0;i<response.Length;i = i + 1024){ filewonloadersList.Add(new FileDownloader("http://stackoverflow.com/robots.txt",i,1024)); }

你的程序将在列表中初始化X FileDownloader(这里没有把这个逻辑放在这里,我关注的是这些东西)

List<Thread> threadList = new List<Thread>(); foreach(FileDownloader aFildeDownloader in filewonloadersList) { Thread aThread = new Thread(aFildeDownloader.DoDownload) //this method will be called when the thread starts threadList.Add(aThread); aThread.Start(); } foreach(Thread aThread in threadList) { aThread.Join();//will wait until the thread is finished } //all the downloader finished their work now you can go through your downloader list and concatenante the temps files

If you're downloading your file in HTTP, you can use this method :

http://msdn.microsoft.com/en-us/library/7fy67z6d.aspx

So you'll have to split your file in multiple temp file and then merge them.

But you'll have to be sure that this feature is enabled on the server side (I don't know if it is by default).

And as some said, you'll gain in performance, that's why Free Download Manager is so useful : it download multiple parts of your file in the same time.

To do this with multi threads :

class FileDownloader{ int Start; int Count; string PathTemp; string Url; FileDownloader(url,start,count){ url = Url; Start =start; Count = count; PathTemp = Path.GetTempFileName() } void DoDownload(){ //do your thing with stream and request and save it to PathTemp } }

Here is your code to initialize your downloader list :

List<FileDownloader> filewonloadersList = new ListFileDownloader>(); System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://stackoverflow.com/robots.txt"); req.Method = "HEAD"; System.Net.WebResponse resp = req.GetResponse(); int responseLength = int.Parse(resp.Headers.Get("Content-Length")); for(int i = 0;i<response.Length;i = i + 1024){ filewonloadersList.Add(new FileDownloader("http://stackoverflow.com/robots.txt",i,1024)); }

And your program will initialize X FileDownloader in a list (didn't put this logic here, I'm focusing on the thready stuff)

List<Thread> threadList = new List<Thread>(); foreach(FileDownloader aFildeDownloader in filewonloadersList) { Thread aThread = new Thread(aFildeDownloader.DoDownload) //this method will be called when the thread starts threadList.Add(aThread); aThread.Start(); } foreach(Thread aThread in threadList) { aThread.Join();//will wait until the thread is finished } //all the downloader finished their work now you can go through your downloader list and concatenante the temps files

更多推荐