同时下载任意多个文件的小程序

昨天要下载一个网站的大量资源,20000个小文件。不用考虑就使用flashget,导入地址列表很慢不说,同时只能下载8个文件,如果是大文件还可以,因为每个文件都是多线程,但是小文件就慢了,总共只有20多K的速度,我的猫可以上200KB的哦,印象中.net实现下载一个文件很简单得,于是赶紧上网找了点资料,改成多线程同时下载100个文件的小程序,速度飞起来了。可能有些地方不完善。

using  System;
using  System.Net;
using  System.IO;
using  System.Threading;
using  System.Collections;

public   class  Downloader
{
    
bool DownOne(string URL, string SavePath)
    
{
        
try
        
{
            Uri url 
= new Uri(URL);
            HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create(url);
            request.Method 
= "GET";
            HttpWebResponse response 
= (HttpWebResponse)request.GetResponse();

            
if (!SavePath.EndsWith(@"")) SavePath += @"";
            Stream reader 
= response.GetResponseStream();
            FileStream writer 
= new FileStream(SavePath + Path.GetFileName(URL), FileMode.Create, FileAccess.Write);

            
int len = (int)response.ContentLength;
            
int downed = 0;
            
byte[] buffer = new byte[len];
            
while (downed < len)
            
{
                
int readed = reader.Read(buffer, 0, buffer.Length);
                downed 
+= readed;
                
if (readed > 0)
                    writer.Write(buffer, 
0, readed);
                
//float percent = (float)downed / (float)len * 100;
                
//Console.WriteLine("当前下载文件大小:" + len + "字节   当前下载大小:" + downed + "字节 下载进度" + percent + "%");
            }


            reader.Close();
            writer.Close();
            response.Close();
            
return true;
        }

        
catch (Exception err)
        
{
            Console.WriteLine(err.Message);

            
try
            
{
                
if (File.Exists(SavePath + Path.GetFileName(URL)))
                    File.Delete(SavePath 
+ Path.GetFileName(URL));
            }

            
catch { }
            
return false;
        }

    }


    
public Downloader(int max, int sleep)
    
{
        
this._MaxThread = max;
        
this._SleepTime = sleep;
    }


    
int _MaxThread;
    
int _SleepTime;

    
int _DowningCount;
    
int DowningCount
    
{
        
get lock (thisreturn this._DowningCount; } }
        
set lock (thisthis._DowningCount = value; } }
    }


    
int _StartOne;
    
int StartOne
    
{
        
get lock (thisreturn this._StartOne; } }
        
set lock (thisthis._StartOne = value; } }
    }

    
string[] _URLS;
    
string _SavePath;
    
void _DownMoreOne()
    
{
        
string url = this._URLS[this.StartOne++];
        
this.DowningCount++;
        
if (DownOne(url, this._SavePath))
            Console.WriteLine(url 
+ "下载成功");
        
else
            Console.WriteLine(url 
+ "下载失败");
        
this.DowningCount--;
    }


    
public void DownAll(string[] URLS, string SavePath)
    
{
        
this._URLS = URLS;
        
this._SavePath = SavePath;
        
this._StartOne = 0;

        
while (this.StartOne < URLS.Length)
        
{
            
if (this.DowningCount < this._MaxThread)
            
{
                
new Thread(new ThreadStart(this._DownMoreOne)).Start();
            }

            Thread.Sleep(
this._SleepTime);
        }

    }

    
public void DownList(string lstFile, string SavePath)
    
{
        ArrayList list 
= new ArrayList();
        
string line;
        StreamReader reader 
= new StreamReader(lstFile);
        
while ((line = reader.ReadLine()) != null)
            list.Add(line);
        
string[] urls = new string[list.Count];
        list.CopyTo(urls);
        DownAll(urls, SavePath);
    }

}


class  Program
{
    
static void Main(string[] args)
    
{
        Downloader d 
= new Downloader(10010);
        d.DownList(
@"E:/URL/all.txt"@"E:/save");
    }

}

技术讨论的QQ群: 2514097 或 10987609 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值