C# 多线程下载

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;
        }
    }
}

 

posted @ 2014-12-23 20:26  大空白纸  阅读(394)  评论(0编辑  收藏  举报