• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#多线程下载

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Net;
using System.IO;

namespace WebAppTest
{
    public partial class download : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //断点下载大文件
            //原理:使用多个线程将一个文件分成多个文件进行下载
            MutilDownload();
        }


        public void MutilDownload()
        {
            int threadcount = 3;
            int threadCompleteNum = 0;

            string[] files = new string[threadcount];
            int total = (int)GetDownloadFileSize();

            int per = total / threadcount;


            for (int i = 1; i <= threadcount; i++)
            {

                int from = (i - 1) * per+1;
                int to = i * per;
                if (i == 1)
                {
                    from = 0;
                }

                if (i == threadcount)
                {
                    to = total;
                }
               
                string path = Server.MapPath("~/resource/video/");

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                int index = i;

                Thread thread = new Thread(() =>
                {


                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://192.168.1.100:106/test/test.avi");

                    request.Method = "get";

                    request.AddRange(from, to);


                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                    string filename = path + index + ".avi";

                    files[index-1] = filename;

                    using (FileStream fs = new FileStream(filename, FileMode.CreateNew))
                    {
                        byte[] buffer = new byte[5 * 1024 * 1024];
                        int r;

                        using (Stream stream = response.GetResponseStream())
                        {
                            while ((r = stream.Read(buffer, 0, buffer.Length)) != 0)
                            {

                                fs.Write(buffer, 0, r);

                            }
                        }

                    }

                    threadCompleteNum++;


                    if (threadCompleteNum == threadcount)
                    {
                        //和并临时文件
                        string allFilename = path + "all.avi";

                        using (FileStream allFile = new FileStream(allFilename, FileMode.CreateNew))
                        {

                            for (int j = 0; j < files.Length; j++)
                            {
                                using (FileStream tempFile = new FileStream(files[j], FileMode.Open))
                                {
                                    byte[] tempBuffer = new byte[5 * 1024 * 1024];
                                    int t;

                                    while ((t = tempFile.Read(tempBuffer, 0, tempBuffer.Length)) != 0)
                                    {
                                        allFile.Write(tempBuffer, 0, t);
                                    }

                                }

                               
                            }
                        }

                        //删除临时文件
                        foreach (var f in files)
                        {
                            if (File.Exists(f))
                            {
                                File.Delete(f);
                            } 
                        }

                    }

                });

                thread.IsBackground = true;

                thread.Start();


            }

        }

        //获取下载文件大小
        public long GetDownloadFileSize()
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://192.168.1.100:106/test/test.avi");

            request.Method = "get";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            return response.ContentLength;
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[C#]GetFloat提示&quot;指定的转换无效&quot;发布时间:2022-07-13
下一篇:
黄聪:用C#为Excel创建个人宏工作薄(原创)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap