Пример #1
0
        public override void Start()
        {
            if (!(DownLoadUrl != null && FilePath != null && FileName != null && ThreadNum != 0))
            {
                return;
            }
            Running = true;
            try
            {
                State = "下载中";
                HttpWebRequest  httpWebRequest  = (HttpWebRequest)WebRequest.Create(DownLoadUrl);
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                contentLength = httpWebResponse.ContentLength;
                threads       = new DownloadThread[ThreadNum];
                long q = contentLength / ThreadNum;
                new Thread(SpeedStatistics).Start();
                for (int i = 0; i < ThreadNum; i++)
                {
                    if (i == (ThreadNum - 1))
                    {
                        threads[i] = new DownloadThread
                        {
                            Url      = DownLoadUrl,
                            FileName = FilePath + "\\" + FileName + "._tmp" + i.ToString(),
                            From     = q * i,
                            To       = contentLength,
                            download = this
                        };
                        break;
                    }

                    threads[i] = new DownloadThread
                    {
                        Url      = DownLoadUrl,
                        FileName = FilePath + "\\" + FileName + "._tmp" + i.ToString(),
                        From     = q * i,
                        To       = (q * (i + 1)) - 1,
                        download = this
                    };
                }
            }
            catch (Exception ex)
            {
                State = "下载失败";
                SetComplete();
                MessageBox.Show($"下载失败! 错误: {ex.Message} \r\n如果以上错误为404的话 那就是文件名非法\r\n百度云的API并不允许下载有特殊字符的文件名");
            }
        }
Пример #2
0
 private void HttpDownload_DownloadCompletedEvent(DownloadThread thread)
 {
     lock (this)
     {
         bool flag = false;
         foreach (DownloadThread thr in threads)
         {
             if (thr.Equals(thread))
             {
                 continue;
             }
             if (!thr.Completed)
             {
                 flag = true;
             }
         }
         if (flag)
         {
             return;
         }
         if (Stop)
         {
             return;
         }
         string[] files = new string[threads.Length];
         for (int i = 0; i < threads.Length; i++)
         {
             files[i] = threads[i].DownloadPath;
         }
         State = TaskState.合并文件中;
         FileOperation.CombineFiles(files, FilePath + "\\" + FileName);
         Running      = false;
         TaskComplete = true;
         State        = TaskState.载完成;
         TaskCompletedEvent?.Invoke();
     }
 }
Пример #3
0
 public override void Start()
 {
     if (!(DownLoadUrl != null && FilePath != null && FileName != null && ThreadNum != 0))
     {
         return;
     }
     Running = true;
     try
     {
         State = TaskState.载中;
         HttpWebRequest  httpWebRequest  = (HttpWebRequest)WebRequest.Create(DownLoadUrl);
         HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
         contentLength = httpWebResponse.ContentLength;
         threads       = new DownloadThread[ThreadNum];
         if (From == To)
         {
             From = 0;
             To   = contentLength;
         }
         else
         {
             contentLength = To - From;
         }
         long q = contentLength / ThreadNum;
         for (int i = 0; i < ThreadNum; i++)
         {
             if (i == (ThreadNum - 1))
             {
                 threads[i] = new DownloadThread
                 {
                     Url          = DownLoadUrl,
                     DownloadPath = FilePath + "\\" + FileName + "._tmp" + i.ToString(),
                     From         = From + (q * i),
                     To           = To
                 };
                 threads[i].DownloadCompletedEvent += HttpDownload_DownloadCompletedEvent;
                 break;
             }
             threads[i] = new DownloadThread
             {
                 Url          = DownLoadUrl,
                 DownloadPath = FilePath + "\\" + FileName + "._tmp" + i.ToString(),
                 From         = From + (q * i),
                 To           = From + (q * (i + 1) - 1),
             };
             threads[i].DownloadCompletedEvent += HttpDownload_DownloadCompletedEvent;
         }
     }
     catch (Exception ex)
     {
         State = TaskState.任务失败;
         SetComplete();
         if (ex is WebException)
         {
             //404 ERROR
             if (ex.Message.Contains("404"))
             {
                 MessageBox.Show("下载失败! 文件名有非法字符,请使用云管家重命名后再下载");
                 return;
             }
             //403 ERROR
             if (ex.Message.Contains("403"))
             {
                 MessageBox.Show("下载失败! 百度抽风了,你可以换个账号或者等几天再下载\r\nPS:一般来说等一天就好");
                 return;
             }
         }
         MessageBox.Show($"下载失败! 错误: {ex.Message}");
     }
 }