123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using Common.Model;
-
- using System;
- using System.IO;
- using System.Net;
-
- namespace Common.system
- {
-
-
-
-
-
- public class DownloadService
- {
- private string downloadUrl = "";
- private string filePath = "";
- private string method = "";
- private long fromIndex = 0;
- private long toIndex = 0;
- private long count = 0;
- private long size = 524288;
- private bool isRun = false;
-
-
- public bool isFinish = false;
-
- public bool isStopped = true;
-
-
-
-
- public event Action OnStart;
-
-
-
- public event Action OnDownload;
-
-
-
- public event Action OnFinsh;
-
-
-
- public event Action OnDisconnect;
-
-
- public long GetDownloadedCount()
- {
- return count - toIndex + fromIndex - 1;
- }
-
- public void Stop()
- {
- isRun = false;
- }
-
-
-
-
-
-
-
- public bool Start(TaskInfoModel info, bool isReStart)
- {
- downloadUrl = info.downloadUrl;
- fromIndex = info.fromIndex;
- toIndex = info.toIndex;
- method = info.method;
- filePath = info.filePath;
- count = info.count;
- isStopped = false;
- if (File.Exists(filePath))
- {
- if (isReStart)
- {
- File.Delete(filePath);
- File.Create(filePath).Close();
- }
- }
- else
- {
- File.Create(filePath).Close();
- }
- using (FileStream file = File.Open(filePath, FileMode.Open))
- {
- fromIndex = info.fromIndex + file.Length;
- }
- if (fromIndex >= toIndex)
- {
- OnFineshHandler();
- isFinish = true;
- isStopped = true;
- return false;
- }
- OnStartHandler();
- isRun = true;
- new Action(() =>
- {
- WebResponse rsp;
- while (fromIndex < toIndex && isRun)
- {
- long to;
- if (fromIndex + size >= toIndex - 1)
- {
- to = toIndex - 1;
- }
- else
- {
- to = fromIndex + size;
- }
-
- try
- {
- using (rsp = HttpHelper.Download(downloadUrl, fromIndex, to, method))
- {
- Save(filePath, rsp.GetResponseStream());
- }
- }
- catch (Exception ex)
- {
- string ErrMessage = "【下载】(DownloadService):" + ex.Message;
- LogHelper.WriteErrLog(ErrMessage, ex);
- OnDisconnectHandler();
- return;
- }
- }
- if (!isRun)
- {
- isStopped = true;
- }
-
- if (fromIndex >= toIndex)
- {
- isFinish = true;
- isStopped = true;
- OnFineshHandler();
- }
-
- }).BeginInvoke(null, null);
- return true;
- }
-
-
-
-
-
- private void Save(string filePath, Stream stream)
- {
- try
- {
- using (FileStream writer = File.Open(filePath, FileMode.Append))
- {
- using (stream)
- {
- int repeatTimes = 0;
- byte[] buffer = new byte[1024];
- int length = 0;
- while ((length = stream.Read(buffer, 0, buffer.Length)) > 0 && isRun)
- {
- writer.Write(buffer, 0, length);
- fromIndex += length;
- if (repeatTimes % 5 == 0)
- {
- OnDownloadHandler();
- }
- repeatTimes++;
- }
- }
- }
- OnDownloadHandler();
- }
- catch (Exception)
- {
-
- }
- }
-
-
-
- private void OnStartHandler()
- {
- new Action(() =>
- {
- if (OnStart != null)
- {
- OnStart.Invoke();
- }
- }).BeginInvoke(null, null);
- }
-
-
-
- private void OnFineshHandler()
- {
- new Action(() =>
- {
- if (OnFinsh != null)
- {
- OnFinsh.Invoke();
- }
-
- if (OnDownload != null)
- {
- OnDownload.Invoke();
- }
- }).BeginInvoke(null, null);
- }
-
-
-
- private void OnDownloadHandler()
- {
- new Action(() =>
- {
- if (OnDownload != null)
- {
- OnDownload.Invoke();
- }
- }).BeginInvoke(null, null);
- }
-
-
-
- private void OnDisconnectHandler()
- {
- new Action(() =>
- {
- if (OnDisconnect != null)
- {
- OnDisconnect.Invoke();
- }
- }).BeginInvoke(null, null);
-
- }
- }
- }
|