星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DownloadService.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using Common.Model;
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. namespace Common.system
  6. {
  7. /// <summary>
  8. /// 下载方法
  9. /// 创建人:赵耀
  10. /// 创建时间:2018年10月30日
  11. /// </summary>
  12. public class DownloadService
  13. {
  14. private string _downloadUrl = ""; //文件下载地址
  15. private string _filePath = ""; //文件保存路径
  16. private string _method = ""; //方法
  17. private long _fromIndex; //开始下载的位置
  18. private long _toIndex; //结束下载的位置
  19. private long _count; //总大小
  20. private readonly long _size = 524288; //每次下载大小 512kb
  21. private bool _isRun; //是否正在进行
  22. public bool isFinish; //是否已下载完成{ get; private set; }
  23. public bool isStopped = true;//是否已停止{ get; private set; }
  24. /// <summary>
  25. /// 开始下载
  26. /// </summary>
  27. public event Action OnStart;
  28. /// <summary>
  29. /// 下载进度
  30. /// </summary>
  31. public event Action OnDownload;
  32. /// <summary>
  33. /// 完成下载
  34. /// </summary>
  35. public event Action OnFinsh;
  36. /// <summary>
  37. /// 断开网络连接
  38. /// </summary>
  39. public event Action OnDisconnect;
  40. public long GetDownloadedCount()
  41. {
  42. return _count - _toIndex + _fromIndex - 1;
  43. }
  44. public void Stop()
  45. {
  46. _isRun = false;
  47. }
  48. /// <summary>
  49. /// 开始下载
  50. /// </summary>
  51. /// <param name="info"></param>
  52. /// <param name="isReStart"></param>
  53. /// <returns></returns>
  54. public bool Start(TaskInfoModel info, bool isReStart)
  55. {
  56. _downloadUrl = info.downloadUrl;
  57. _fromIndex = info.fromIndex;
  58. _toIndex = info.toIndex;
  59. _method = info.method;
  60. _filePath = info.filePath;
  61. _count = info.count;
  62. isStopped = false;
  63. if (File.Exists(_filePath))
  64. {
  65. if (isReStart)
  66. {
  67. File.Delete(_filePath);
  68. File.Create(_filePath).Close();
  69. }
  70. }
  71. else
  72. {
  73. File.Create(_filePath).Close();
  74. }
  75. using (FileStream file = File.Open(_filePath, FileMode.Open))
  76. {
  77. _fromIndex = info.fromIndex + file.Length;
  78. }
  79. if (_fromIndex >= _toIndex)
  80. {
  81. OnFineshHandler();
  82. isFinish = true;
  83. isStopped = true;
  84. return false;
  85. }
  86. OnStartHandler();
  87. _isRun = true;
  88. new Action(() =>
  89. {
  90. while (_fromIndex < _toIndex && _isRun)
  91. {
  92. long to;
  93. if (_fromIndex + _size >= _toIndex - 1)
  94. {
  95. to = _toIndex - 1;
  96. }
  97. else
  98. {
  99. to = _fromIndex + _size;
  100. }
  101. try
  102. {
  103. WebResponse rsp;
  104. using (rsp = ZHttpUtil.Download(
  105. _downloadUrl,
  106. _fromIndex,
  107. to,
  108. _method
  109. ))
  110. {
  111. Save(_filePath, rsp.GetResponseStream());
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. string errMessage = "【下载】(DownloadService):" + ex.Message;
  117. LogHelper.Logerror.Error(errMessage, ex);
  118. OnDisconnectHandler();
  119. return;
  120. }
  121. }
  122. if (!_isRun)
  123. {
  124. isStopped = true;
  125. }
  126. if (_fromIndex >= _toIndex)
  127. {
  128. isFinish = true;
  129. isStopped = true;
  130. OnFineshHandler();
  131. }
  132. }).BeginInvoke(null, null);
  133. return true;
  134. }
  135. /// <summary>
  136. /// 保存文件
  137. /// </summary>
  138. /// <param name="filePath"></param>
  139. /// <param name="stream"></param>
  140. private void Save(string filePath, Stream stream)
  141. {
  142. try
  143. {
  144. using (FileStream writer = File.Open(filePath, FileMode.Append))
  145. {
  146. using (stream)
  147. {
  148. int repeatTimes = 0;
  149. byte[] buffer = new byte[1024];
  150. int length;
  151. while ((length = stream.Read(buffer, 0, buffer.Length)) > 0 && _isRun)
  152. {
  153. writer.Write(buffer, 0, length);
  154. _fromIndex += length;
  155. if (repeatTimes % 5 == 0)
  156. {
  157. OnDownloadHandler();
  158. }
  159. repeatTimes++;
  160. }
  161. }
  162. }
  163. OnDownloadHandler();
  164. }
  165. catch (Exception)
  166. {
  167. //异常也不影响
  168. }
  169. }
  170. /// <summary>
  171. /// 开始下载
  172. /// </summary>
  173. private void OnStartHandler()
  174. {
  175. new Action(() =>
  176. {
  177. if (OnStart != null)
  178. {
  179. OnStart.Invoke();
  180. }
  181. }).BeginInvoke(null, null);
  182. }
  183. /// <summary>
  184. /// 完成下载
  185. /// </summary>
  186. private void OnFineshHandler()
  187. {
  188. new Action(() =>
  189. {
  190. if (OnFinsh != null)
  191. {
  192. OnFinsh.Invoke();
  193. }
  194. if (OnDownload != null)
  195. {
  196. OnDownload.Invoke();
  197. }
  198. }).BeginInvoke(null, null);
  199. }
  200. /// <summary>
  201. /// 下载进度
  202. /// </summary>
  203. private void OnDownloadHandler()
  204. {
  205. new Action(() =>
  206. {
  207. if (OnDownload != null)
  208. {
  209. OnDownload.Invoke();
  210. }
  211. }).BeginInvoke(null, null);
  212. }
  213. /// <summary>
  214. /// 断开网络连接
  215. /// </summary>
  216. private void OnDisconnectHandler()
  217. {
  218. new Action(() =>
  219. {
  220. if (OnDisconnect != null)
  221. {
  222. OnDisconnect.Invoke();
  223. }
  224. }).BeginInvoke(null, null);
  225. }
  226. }
  227. }