昨天要下载一个网站的大量资源,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 (this) ...{ return this._DowningCount; } }
set ...{ lock (this) ...{ this._DowningCount = value; } }
}
int _StartOne;
int StartOne
...{
get ...{ lock (this) ...{ return this._StartOne; } }
set ...{ lock (this) ...{ this._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(100, 10);
d.DownList(@"E:/URL/all.txt", @"E:/save");
}
}
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 (this) ...{ return this._DowningCount; } }
set ...{ lock (this) ...{ this._DowningCount = value; } }
}
int _StartOne;
int StartOne
...{
get ...{ lock (this) ...{ return this._StartOne; } }
set ...{ lock (this) ...{ this._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(100, 10);
d.DownList(@"E:/URL/all.txt", @"E:/save");
}
}
技术讨论的QQ群: 2514097 或 10987609