FTPTool.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using Azylee.Core.DelegateUtils.ProcessDelegateUtils;
  6. using Azylee.Core.IOUtils.DirUtils;
  7. using System;
  8. using System.IO;
  9. using System.Net;
  10. namespace Azylee.YeahWeb.FTPUtils
  11. {
  12. /// <summary>
  13. /// FTP 帮助类
  14. /// </summary>
  15. public class FtpTool
  16. {
  17. private string HostIP { get; set; }
  18. private string UserName { get; set; }
  19. private string Password { get; set; }
  20. private string FtpUri { get { return $@"ftp://{HostIP}/"; } }
  21. public FtpTool(string ftpHostIP, string username, string password)
  22. {
  23. this.HostIP = ftpHostIP;
  24. this.UserName = username;
  25. this.Password = password;
  26. }
  27. private FtpWebRequest GetRequest(string uri)
  28. {
  29. //根据服务器信息FtpWebRequest创建类的对象
  30. FtpWebRequest result = (FtpWebRequest)WebRequest.Create(uri);
  31. result.Credentials = new NetworkCredential(UserName, Password);
  32. result.KeepAlive = false;
  33. result.UsePassive = false;
  34. result.UseBinary = true;
  35. //request.Proxy = this.proxy;
  36. result.EnableSsl = false;
  37. return result;
  38. }
  39. public bool DownloadFile(string ftpFilePath, string saveDir)
  40. {
  41. try
  42. {
  43. string filename = ftpFilePath.Substring(ftpFilePath.LastIndexOf("\\") + 1);
  44. string tmpname = Guid.NewGuid().ToString();
  45. string uri = Path.Combine(FtpUri, ftpFilePath);
  46. if (!Directory.Exists(saveDir)) Directory.CreateDirectory(saveDir);
  47. FtpWebRequest ftp = GetRequest(uri);
  48. ftp.Method = WebRequestMethods.Ftp.DownloadFile;
  49. using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
  50. {
  51. using (Stream responseStream = response.GetResponseStream())
  52. {
  53. using (FileStream fs = new FileStream(Path.Combine(saveDir, filename), FileMode.CreateNew))
  54. {
  55. byte[] buffer = new byte[2048];
  56. int read = 0;
  57. do
  58. {
  59. read = responseStream.Read(buffer, 0, buffer.Length);
  60. fs.Write(buffer, 0, read);
  61. } while (!(read == 0));
  62. fs.Flush();
  63. }
  64. }
  65. }
  66. return true;
  67. }
  68. catch { }
  69. return false;
  70. }
  71. public long GetFileSize(string ftpFile)
  72. {
  73. long result = 0;
  74. try
  75. {
  76. string uri = Path.Combine(FtpUri, ftpFile);
  77. FtpWebRequest request = GetRequest(uri);
  78. request.Method = WebRequestMethods.Ftp.GetFileSize;
  79. using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
  80. {
  81. result = response.ContentLength;
  82. }
  83. }
  84. catch (Exception e) { }
  85. return result;
  86. }
  87. public bool Download(string ftpFile, string localFile, ProgressDelegate.ProgressHandler progress = null, object sender = null, bool overwrite = true)
  88. {
  89. try
  90. {
  91. long current = 0, filesize = GetFileSize(ftpFile);
  92. string localPath = DirTool.GetFilePath(localFile);
  93. if (!Directory.Exists(localPath)) Directory.CreateDirectory(localPath);
  94. string uri = Path.Combine(FtpUri, ftpFile);
  95. FtpWebRequest ftp = GetRequest(uri);
  96. ftp.Method = WebRequestMethods.Ftp.DownloadFile;
  97. using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
  98. {
  99. using (Stream responseStream = response.GetResponseStream())
  100. {
  101. using (FileStream fs = new FileStream(localFile, FileMode.Create))
  102. {
  103. byte[] buffer = new byte[1024 * 1024];
  104. int read = 0;
  105. do
  106. {
  107. read = responseStream.Read(buffer, 0, buffer.Length);
  108. fs.Write(buffer, 0, read);
  109. current += read;
  110. progress?.Invoke(sender, new ProgressEventArgs(current, filesize));
  111. } while (!(read == 0));
  112. fs.Flush();
  113. }
  114. }
  115. }
  116. return true;
  117. }
  118. catch { }
  119. return false;
  120. }
  121. }
  122. }