DownloadThread.cs 3.9 KB
Newer Older
林新发's avatar
林新发 已提交
1
using System;
林新发's avatar
林新发 已提交
2 3
using System.IO;
using System.Net;
林新发's avatar
林新发 已提交
4 5
using System.Security.Cryptography;
using System.Text;
林新发's avatar
林新发 已提交
6 7 8 9 10

namespace winform1
{
    class DownloadThread
    {
林新发's avatar
林新发 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        public delegate void EventDownloadStart(long totalSize);
        public delegate void EventDownloadIng(long curDownloadSize);
        public delegate void EventCheckingMd5();
        public delegate void EventDownloadDone();

        public event EventDownloadStart eventDownloadStart;
        public event EventDownloadIng eventDownloadIng;
        public event EventCheckingMd5 eventCheckingMd5;
        public event EventDownloadDone eventDownloadDone;

        public static string GetMD5FromFile(string filename)
        {
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Open);
                MD5CryptoServiceProvider md5Helper = new MD5CryptoServiceProvider();
                byte[] data = md5Helper.ComputeHash(fs);
                fs.Close();
                StringBuilder sbr = new StringBuilder();
                for (int i = 0; i < data.Length; ++i)
                {
                    sbr.Append(data[i].ToString("X2"));
                }
                string md5Str = sbr.ToString();
                return md5Str;
            }
            catch (Exception)
            {
                throw;
            }
        }
林新发's avatar
林新发 已提交
42 43 44 45 46 47

        /// <summary>        
        /// c#,.net 下载文件        
        /// </summary>        
        /// <param name="url">下载文件地址</param>       
        /// <param name="filename">下载后的存放地址</param>     
林新发's avatar
林新发 已提交
48
        public void RunMethod(string url, string filename, long totalSize, string md5)
林新发's avatar
林新发 已提交
49 50 51
        {
            try
            {
林新发's avatar
林新发 已提交
52 53 54
                Stream st;
                Stream so;

林新发's avatar
林新发 已提交
55
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
林新发's avatar
林新发 已提交
56 57
                request.Timeout = 10000;

林新发's avatar
林新发 已提交
58
                //通知主界面,我开始了, response.ContentLength用来设置进度条的最大值
林新发's avatar
林新发 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                eventDownloadStart(totalSize);

                if (File.Exists(filename))
                {
                    //续传
                    so = File.OpenWrite(filename);
                    if (so.Length < totalSize)
                    {
                        so.Seek(so.Length, SeekOrigin.Current);
                        request.AddRange((int)so.Length);
                    }
                    else
                    {
                        so.Close();
                        so = new FileStream(filename, FileMode.Create);
                    }

                }
                else
                {
                    so = new FileStream(filename, FileMode.Create);
                }

                long totalDownloadedByte = so.Length;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                st = response.GetResponseStream();
林新发's avatar
林新发 已提交
86 87 88 89 90 91 92 93 94 95

                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte += osize;
                    so.Write(by, 0, osize);
                    osize = st.Read(by, 0, by.Length);

                    //通知主界面我正在执行,totalDownloadedByte表示进度条当前进度
林新发's avatar
林新发 已提交
96
                    eventDownloadIng(totalDownloadedByte);
林新发's avatar
林新发 已提交
97 98 99 100
                }
                so.Close();
                st.Close();

林新发's avatar
林新发 已提交
101 102 103 104 105 106 107 108 109 110 111
                eventCheckingMd5();
                if (GetMD5FromFile(filename) != md5)
                {
                    //md5校验不通过,重新传
                    RunMethod(url, filename, totalSize, md5);
                }
                else
                {
                    //通知主界面我已经完成了
                    eventDownloadDone();
                }
林新发's avatar
林新发 已提交
112 113 114 115 116 117 118 119
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}