WebFileDown.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. namespace Falit.Run
  9. {
  10. /// <summary>
  11. /// Web下载文件(单线程下载,速度慢)
  12. /// </summary>
  13. public static class WebFileDown
  14. {
  15. /// 下载文件方法
  16. /// 文件保存路径和文件名
  17. /// 返回服务器文件名
  18. public static bool DeownloadFile(string sourceFile, string desFile, int dataIndex,
  19. System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1, System.Windows.Forms.DataGridView dataGridView)
  20. {
  21. float percent = 0;
  22. long sPosition = 0;
  23. FileStream FStream = null;
  24. Stream myStream = null;
  25. string fileName = sourceFile.Substring(sourceFile.LastIndexOf(@"/") + 1);
  26. try
  27. {
  28. if (File.Exists(desFile))
  29. {
  30. FStream = File.OpenWrite(desFile);
  31. sPosition = FStream.Length;
  32. long serverFileLength = GetHttpLength(sourceFile);
  33. if (sPosition == serverFileLength)
  34. {
  35. dataGridView.Rows[dataIndex].DefaultCellStyle.ForeColor = Color.Red;
  36. dataGridView.Rows[dataIndex].Cells["进度"].Value = "100%";
  37. return true;
  38. }
  39. FStream.Seek(sPosition, SeekOrigin.Current);
  40. }
  41. else
  42. {
  43. //文件不保存创建一个文件
  44. FStream = new FileStream(desFile, FileMode.Create);
  45. sPosition = 0;
  46. }
  47. HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(sourceFile);
  48. System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)myRequest.GetResponse();
  49. if (sPosition > 0)
  50. {
  51. myRequest.AddRange(sPosition);
  52. }
  53. long totalBytes = myrp.ContentLength;
  54. myStream = myRequest.GetResponse().GetResponseStream();
  55. byte[] btContent = new byte[512];
  56. int intSize = 0;
  57. long totalDownloadedByte = 0;
  58. intSize = myStream.Read(btContent, 0, 512);
  59. while (intSize > 0)
  60. {
  61. FStream.Write(btContent, 0, intSize);
  62. intSize = myStream.Read(btContent, 0, 512);
  63. totalDownloadedByte = intSize + totalDownloadedByte;
  64. dataGridView.Rows[dataIndex].Cells["进度"].Value = (float)totalDownloadedByte / (float)totalBytes * 100 + "%";
  65. percent = (float)totalDownloadedByte / (float)totalBytes * 100;
  66. label1.Text = percent.ToString("0.00") + "%";
  67. //System.Windows.Forms.Application.DoEvents();
  68. }
  69. return true;
  70. }
  71. catch (Exception ex)
  72. {
  73. return true;
  74. }
  75. finally
  76. {
  77. if (myStream != null)
  78. {
  79. myStream.Close();
  80. myStream.Dispose();
  81. }
  82. if (FStream != null)
  83. {
  84. FStream.Close();
  85. FStream.Dispose();
  86. }
  87. }
  88. }
  89. static long GetHttpLength(string url)
  90. {
  91. long length = 0;
  92. try
  93. {
  94. var req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
  95. req.Method = "HEAD";
  96. req.Timeout = 5000;
  97. var res = (HttpWebResponse)req.GetResponse();
  98. if (res.StatusCode == HttpStatusCode.OK)
  99. {
  100. length = res.ContentLength;
  101. }
  102. res.Close();
  103. return length;
  104. }
  105. catch (WebException wex)
  106. {
  107. throw wex;
  108. }
  109. }
  110. }
  111. }