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

DownloadUtil.cs 6.2KB

hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Threading;
  5. using System.Windows.Threading;
  6. namespace XHZB.Desktop.Utils
  7. {
  8. internal class DownloadUtil
  9. {
  10. public static void downloadFile(string url, string savepath)
  11. {
  12. string path = savepath;
  13. string filename = url.Substring(url.LastIndexOf("/") + 1);
  14. if (!url.StartsWith("http") || filename == null || filename == "")
  15. {
  16. return;
  17. }
  18. path += filename;
  19. // 设置参数
  20. HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  21. //发送请求并获取相应回应数据
  22. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  23. //直到request.GetResponse()程序才开始向目标网页发送Post请求
  24. Stream responseStream = response.GetResponseStream();
  25. //创建本地文件写入流
  26. Stream stream = new FileStream(path, FileMode.Create);
  27. byte[] bArr = new byte[1024];
  28. int size = responseStream.Read(bArr, 0, bArr.Length);
  29. while (size > 0)
  30. {
  31. stream.Write(bArr, 0, size);
  32. size = responseStream.Read(bArr, 0, bArr.Length);
  33. }
  34. stream.Close();
  35. responseStream.Close();
  36. }
  37. /// <summary>
  38. /// Http下载文件
  39. /// </summary>
  40. public static Thread downloadFileWithCallback(
  41. string url,
  42. int position,
  43. string savepath,
  44. Dispatcher dispatcher,
  45. DownloadCallback callback
  46. )
  47. {
  48. string path = savepath;
  49. string filename = url.Substring(url.LastIndexOf("/") + 1);
  50. path += filename;
  51. if (!url.StartsWith("http") || filename == null || filename == "")
  52. {
  53. return null;
  54. }
  55. Thread thread = new Thread(o =>
  56. {
  57. FileInfo fi = new FileInfo(path);
  58. if (fi.Exists)
  59. {
  60. dispatcher.Invoke(new Action(() =>
  61. {
  62. callback.downloadEnd(position, path);
  63. }));
  64. }
  65. else
  66. {
  67. string temppath = path + ".tmp";
  68. if (File.Exists(temppath))
  69. {
  70. //File.Delete(temppath);
  71. temppath += "1";
  72. while (File.Exists(temppath))
  73. {
  74. temppath += "1";
  75. if (!File.Exists(temppath))
  76. {
  77. break;
  78. }
  79. }
  80. }
  81. dispatcher.Invoke(new Action(() =>
  82. {
  83. callback.downloadBegin(position);
  84. }));
  85. try
  86. {
  87. HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  88. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  89. Stream responseStream = response.GetResponseStream();
  90. //创建本地文件写入流
  91. Stream stream = new FileStream(temppath, FileMode.Create);
  92. long totalSize = response.ContentLength;
  93. byte[] bArr = new byte[1024];
  94. int size = responseStream.Read(bArr, 0, bArr.Length);
  95. long readsize = 0;
  96. while (size > 0)
  97. {
  98. readsize += size;
  99. stream.Write(bArr, 0, size);
  100. size = responseStream.Read(bArr, 0, bArr.Length);
  101. dispatcher.Invoke(new Action(() =>
  102. {
  103. callback.downloadProgress(position, (int)(readsize * 100 / totalSize));
  104. }));
  105. }
  106. stream.Close();
  107. responseStream.Close();
  108. FileInfo fitemp = new FileInfo(temppath);
  109. fitemp.MoveTo(path);
  110. dispatcher.Invoke(new Action(() =>
  111. {
  112. callback.downloadEnd(position, path);
  113. }));
  114. }
  115. catch (Exception ex)
  116. {
  117. dispatcher.Invoke(new Action(() =>
  118. {
  119. callback.downloadError(position, ex.Message);
  120. }));
  121. }
  122. }
  123. });
  124. thread.Start();
  125. return thread;
  126. }
  127. public static void downloadFileWithCallback(
  128. string url,
  129. int position,
  130. Dispatcher dispatcher,
  131. DownloadCallback callback
  132. )
  133. {
  134. string path = APP.tempPath;
  135. downloadFileWithCallback(url, position, path, dispatcher, callback);
  136. }
  137. public interface DownloadCallback
  138. {
  139. /// <summary>
  140. /// 下载开始
  141. /// </summary>
  142. /// <param name="position"></param>
  143. void downloadBegin(int position);
  144. /// <summary>
  145. /// 下载过程
  146. /// </summary>
  147. /// <param name="position"></param>
  148. /// <param name="progress"></param>
  149. void downloadProgress(int position, int progress);
  150. /// <summary>
  151. /// 下载结束
  152. /// </summary>
  153. /// <param name="position"></param>
  154. /// <param name="filepath"></param>
  155. void downloadEnd(int position, string filepath);
  156. /// <summary>
  157. /// 下载结束
  158. /// </summary>
  159. /// <param name="position"></param>
  160. /// <param name="filepath"></param>
  161. void downloadError(int position, string msg);
  162. }
  163. }
  164. }