多线程分段下载DownLoader代码

来源:互联网 发布:同花顺指标公式源码 编辑:程序博客网 时间:2024/05/23 14:04
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;using System.IO;using System.Threading;namespace MyDownLoader{  //界面控件不能由非此创建的线程操作,所以用了一个Deletgate    //启多个线程下载,全部完成后再整合    public class Context    {        public int fileIndex;//每个线程接收文件的序号        public int fileStart;//每个线程接收文件的起始位置        public int fileSize;//每个线程接收文件的大小    }    public partial class Form1 : Form    {        delegate void ShowProgressDelegate(object newPos);        AutoResetEvent[] resets = null;        public string strurl;//接受文件的URL        public bool hb;//文件合并标志        public int tCount;//进程数        HttpWebRequest request = null;        public Form1()        {            InitializeComponent();           // System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;        }        private void button1_Click(object sender, EventArgs e)        {            Intil();        }        private void Intil()        {           // string strUrl = "http://www.jingmaizi.com.img.800cdn.com/pic/1-1-%E5%85%A8%E6%99%AFB.jpg";            string strUrl = textBox1.Text;//"http://tyst.migu.cn/public/ringmaker01/2009%E5%B9%B43%E6%9C%88/%E8%BF%87%E6%9C%9F%E6%8C%AF%E9%93%83%E8%A1%A5%E4%BA%A7%E5%93%81/%E5%85%A8%E6%9B%B2%E8%AF%95%E5%90%AC/Mp3_128_44_16/Extreme%20Ways-Moby%20(%E9%AD%94%E6%AF%94).mp3?msisdn\u003d8ff1025a9cdf";            tCount = Convert.ToInt32(textBox2.Text.Trim().ToString());            object Extension = System.IO.Path.GetExtension(strUrl) as object;            int size = 0;            try            {                request = (HttpWebRequest)HttpWebRequest.Create(strUrl);                size = (int)request.GetResponse().ContentLength;                request.Abort();            }            catch            {                throw;            }            int fileTreadSize = size / tCount;//每个线程需要处理的文件大小,均分            int fileRemains = fileTreadSize + (int)fileTreadSize % tCount;//剩下的交由一个线程处理            resets = new AutoResetEvent[tCount];            for (int i = 0; i < tCount; i++)            {                Context content = new Context();                resets[i] = new AutoResetEvent(false);                content.fileIndex = i;                if (i < tCount - 1)                {                    content.fileStart = fileTreadSize * i;                    content.fileSize = fileTreadSize - 1;                }                else                {                    content.fileStart = fileTreadSize * i;                    content.fileSize = fileRemains - 1;                }                Thread t = new Thread(Receive);                t.Start(content);            }            WaitHandle.WaitAll(resets);            Thread hbth = new Thread(new ParameterizedThreadStart(CombineFiles));            hbth.Start(Extension);            MessageBox.Show("下载完毕");        }        /// <summary>        /// 开始下载文件        /// </summary>        /// <param name="content"></param>        private void Receive(object content)        {            Context Context = (Context)content;            byte[] nbytes = new byte[512];            string DownLoadPath = @"D:\我的文档\Downloads\";                        //string strUrl = "http://www.jingmaizi.com.img.800cdn.com/pic/1-1-%E5%85%A8%E6%99%AFB.jpg";            string strUrl = "http://tyst.migu.cn/public/ringmaker01/2009%E5%B9%B43%E6%9C%88/%E8%BF%87%E6%9C%9F%E6%8C%AF%E9%93%83%E8%A1%A5%E4%BA%A7%E5%93%81/%E5%85%A8%E6%9B%B2%E8%AF%95%E5%90%AC/Mp3_128_44_16/Extreme%20Ways-Moby%20(%E9%AD%94%E6%AF%94).mp3?msisdn\u003d8ff1025a9cdf";            try            {                               FileStream fs = new FileStream(DownLoadPath + Context.fileIndex + ".dat", System.IO.FileMode.Create);                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strUrl);                request.AddRange(Context.fileStart, Context.fileStart + Context.fileSize);                System.IO.Stream ns = request.GetResponse().GetResponseStream();                int nreadsize = ns.Read(nbytes, 0, 512);                Thread _progressThread;                _progressThread = new Thread(new ParameterizedThreadStart(ProgressStart));                _progressThread.Start(Context.fileIndex);                                while (nreadsize > 0)                {                    fs.Write(nbytes, 0, nreadsize);                    nreadsize = ns.Read(nbytes, 0, 512);                }                fs.Close();                ns.Close();                resets[Context.fileIndex].Set();            }            catch (Exception ex)            { MessageBox.Show(ex.ToString()); }            finally            {            }        }        private void ProgressStart(object index)        {            ShowProgress(index);        }        public void ShowProgress(object Index)        {// 判断是否在线程中访问            if (!listView1.InvokeRequired)            {                                listView1.Items.Add(new ListViewItem(new string[] { "线程" + ((int)Index).ToString() + "正在下载..." }));            }            else            {                ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress);                // 如使用Invoke会等到函数调用结束,而BeginInvoke不会等待直接往后走                this.BeginInvoke(showProgress, new object[] { Index });                            }        }        /// <summary>        /// 整合下载的文件        /// </summary>        private void CombineFiles(object extension)        {                    //  fs = new FileStream(textBox2.Text.Trim().ToString(), System.IO.FileMode.Create);            string DownLoadPath = @"D:\我的文档\Downloads\";            string p = @"D:\我的文档\Downloads\";            string pp = p + DateTime.Now.ToString("yyyyMMddHHmss") + ".mp3" ;            string[] files = System.IO.Directory.GetFiles(p, "*.dat");            Array.Sort(files);            System.IO.FileStream fileStreamReader;            //追加流            System.IO.FileStream fileStreamWriter = new System.IO.FileStream(pp, System.IO.FileMode.Create, System.IO.FileAccess.Write);            foreach (string s in files)            {                fileStreamReader = new System.IO.FileStream(s, System.IO.FileMode.Open, System.IO.FileAccess.Read);                byte[] bytes = new byte[fileStreamReader.Length];                fileStreamReader.Read(bytes, 0, bytes.Length);                fileStreamReader.Close();                //byte数组追加                fileStreamWriter.Write(bytes, 0, bytes.Length);            }            fileStreamWriter.Flush();            fileStreamWriter.Close();        }    }}


0 0
原创粉丝点击