星火直播PC
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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 = 0;//开始下载的位置
  18. private long toIndex = 0;//结束下载的位置
  19. private long count = 0;//总大小
  20. private long size = 524288;//每次下载大小 512kb
  21. private bool isRun = false;//是否正在进行
  22. public bool isFinish = false;//是否已下载完成{ 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. WebResponse rsp;
  91. while (fromIndex < toIndex && isRun)
  92. {
  93. long to;
  94. if (fromIndex + size >= toIndex - 1)
  95. {
  96. to = toIndex - 1;
  97. }
  98. else
  99. {
  100. to = fromIndex + size;
  101. }
  102. try
  103. {
  104. using (rsp = HttpHelper.Download(downloadUrl, fromIndex, to, method))
  105. {
  106. Save(filePath, rsp.GetResponseStream());
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. string ErrMessage = "【下载】(DownloadService):" + ex.Message;
  112. LogHelper.WriteErrLog(ErrMessage, ex);
  113. OnDisconnectHandler();
  114. return;
  115. }
  116. }
  117. if (!isRun)
  118. {
  119. isStopped = true;
  120. }
  121. if (fromIndex >= toIndex)
  122. {
  123. isFinish = true;
  124. isStopped = true;
  125. OnFineshHandler();
  126. }
  127. }).BeginInvoke(null, null);
  128. return true;
  129. }
  130. /// <summary>
  131. /// 保存文件
  132. /// </summary>
  133. /// <param name="filePath"></param>
  134. /// <param name="stream"></param>
  135. private void Save(string filePath, Stream stream)
  136. {
  137. try
  138. {
  139. using (FileStream writer = File.Open(filePath, FileMode.Append))
  140. {
  141. using (stream)
  142. {
  143. int repeatTimes = 0;
  144. byte[] buffer = new byte[1024];
  145. int length = 0;
  146. while ((length = stream.Read(buffer, 0, buffer.Length)) > 0 && isRun)
  147. {
  148. writer.Write(buffer, 0, length);
  149. fromIndex += length;
  150. if (repeatTimes % 5 == 0)
  151. {
  152. OnDownloadHandler();
  153. }
  154. repeatTimes++;
  155. }
  156. }
  157. }
  158. OnDownloadHandler();
  159. }
  160. catch (Exception)
  161. {
  162. //异常也不影响
  163. }
  164. }
  165. /// <summary>
  166. /// 开始下载
  167. /// </summary>
  168. private void OnStartHandler()
  169. {
  170. new Action(() =>
  171. {
  172. if (OnStart != null)
  173. {
  174. OnStart.Invoke();
  175. }
  176. }).BeginInvoke(null, null);
  177. }
  178. /// <summary>
  179. /// 完成下载
  180. /// </summary>
  181. private void OnFineshHandler()
  182. {
  183. new Action(() =>
  184. {
  185. if (OnFinsh != null)
  186. {
  187. OnFinsh.Invoke();
  188. }
  189. if (OnDownload != null)
  190. {
  191. OnDownload.Invoke();
  192. }
  193. }).BeginInvoke(null, null);
  194. }
  195. /// <summary>
  196. /// 下载进度
  197. /// </summary>
  198. private void OnDownloadHandler()
  199. {
  200. new Action(() =>
  201. {
  202. if (OnDownload != null)
  203. {
  204. OnDownload.Invoke();
  205. }
  206. }).BeginInvoke(null, null);
  207. }
  208. /// <summary>
  209. /// 断开网络连接
  210. /// </summary>
  211. private void OnDisconnectHandler()
  212. {
  213. new Action(() =>
  214. {
  215. if (OnDisconnect != null)
  216. {
  217. OnDisconnect.Invoke();
  218. }
  219. }).BeginInvoke(null, null);
  220. }
  221. }
  222. }