winform多线程分块下载文件 ----分享

javaoraspx 2014-01-17 04:23:11
加精

public class DownloadThread
{
private string saveFilePath;
private Uri downUrl;
private long block;
private int threadId = -1;
private long downLength;
private bool finish = false;
private FileDownloader downloader;

public DownloadThread(FileDownloader downloader, Uri downUrl, string saveFile, long block, long downLength, int threadId)
{
this.downUrl = downUrl;
this.saveFilePath = saveFile;
this.block = block;
this.downloader = downloader;
this.threadId = threadId;
this.downLength = downLength;
}


public void ThreadRun()
{
//task
Thread td = new Thread(new ThreadStart(() =>
{
Console.WriteLine(threadId.ToString() + " start " + downLength.ToString() + " " + block.ToString());
if (downLength < block)//未下载完成
{
try
{
int startPos = (int)(block * (threadId - 1) + downLength);//开始位置
int endPos = (int)(block * threadId - 1);//结束位置
Console.WriteLine("Thread " + this.threadId + " start download from position " + startPos + " and endwith " + endPos);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(downUrl);
request.Referer = downUrl.ToString();
request.Method = "GET";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
request.AllowAutoRedirect = false;
request.ContentType = "application/octet-stream";
request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.Timeout = 10 * 1000;
request.AllowAutoRedirect = true;
request.AddRange(startPos, endPos);
//Console.WriteLine(request.Headers.ToString());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
WebResponse wb = request.GetResponse();
using (Stream _stream = wb.GetResponseStream())
{
byte[] buffer = new byte[1024 * 50];
long offset = -1;
using (Stream threadfile = new FileStream(this.saveFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
threadfile.Seek(startPos, SeekOrigin.Begin);
while ((offset = _stream.Read(buffer, 0, buffer.Length)) != 0)
{
downloader.append(offset);
threadfile.Write(buffer, 0, (int)offset);
downLength += offset;
downloader.update(this.threadId, downLength);
}
threadfile.Close();
_stream.Close();
Console.WriteLine("Thread " + this.threadId + " download finish");
this.finish = true;
}
}
}
catch (Exception e)
{
this.downLength = -1;
Console.WriteLine("Thread " + this.threadId + ":" + e.Message);
}
}
else
{
Console.WriteLine(downLength);
}
}));
td.IsBackground = true;
td.Start();
}
/// <summary>
/// 下载是否完成
/// </summary>
/// <returns></returns>
public bool isFinish()
{
return finish;
}
/// <summary>
/// 已经下载的内容大小 ;return 如果返回值为-1,代表下载失败
/// </summary>
/// <returns></returns>
public long getDownLength()
{
return downLength;
}

}





不想重新写啦..直接看我博客园的文章吧(不会说我打广告吧.最讨厌了.)
最后有源码
链接地址
...全文
2262 64 打赏 收藏 转发到动态 举报
写回复
用AI写文章
64 条回复
切换为时间正序
请发表友善的回复…
发表回复
黑糊糊2013 2014-02-08
  • 打赏
  • 举报
回复
学习了,多谢分享...还不错...
Magic_cui 2014-02-07
  • 打赏
  • 举报
回复
谢谢分享~~~
慧眼识狗熊 2014-02-07
  • 打赏
  • 举报
回复
用线程的话大量用户同时下载服务器受不了吧。
beyondcj 2014-02-05
  • 打赏
  • 举报
回复
ajdopteronmomo 2014-02-04
  • 打赏
  • 举报
回复
学习了
bob76012 2014-02-03
  • 打赏
  • 举报
回复
很棒的范例 而且还有源码可以参考
青松2 2014-02-01
  • 打赏
  • 举报
回复
llsshh1985 2014-01-29
  • 打赏
  • 举报
回复
Mindly123 2014-01-28
  • 打赏
  • 举报
回复
重新看了一下,感觉和那个收费的IDM很像,感谢你免费提供啊
luckdwj 2014-01-28
  • 打赏
  • 举报
回复
学习了,谢谢楼主分享
lvshuchenyin 2014-01-24
  • 打赏
  • 举报
回复
学习一下。。
liuxingchen249 2014-01-23
  • 打赏
  • 举报
回复
回复于: 2014-01-17 21:46:53 非常不错,最近刚好用到,感谢楼主无私分享。 楼下的,技术贴不顶,写代码一堆bug
javaoraspx 2014-01-23
  • 打赏
  • 举报
回复
引用 51 楼 Joyhen 的回复:
测试了一下好像有问题,开一个线程没有问题,但是开2个或者以上,进度和下载的量不对
不会吧?我debug把下载块都打印了.md5都是一致的..你试试debug下
_VINCE_ 2014-01-22
  • 打赏
  • 举报
回复
感谢分享~~~~
分享成长 2014-01-22
  • 打赏
  • 举报
回复
向牛人学习,并不断进步!
joyhen 2014-01-22
  • 打赏
  • 举报
回复
测试了一下好像有问题,开一个线程没有问题,但是开2个或者以上,进度和下载的量不对
rongxiaojun 2014-01-21
  • 打赏
  • 举报
回复
听说每天回一个帖子会有十分我试试
shuiguoqu 2014-01-21
  • 打赏
  • 举报
回复
收藏收藏
xiongxun 2014-01-20
  • 打赏
  • 举报
回复
还不错额虚心学习
雨雪纷纷 2014-01-20
  • 打赏
  • 举报
回复
好牛X的感觉
加载更多回复(35)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧