FTPTool.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.IO;
  7. using System.Net;
  8. using Y.Utils.IOUtils.PathUtils;
  9. namespace Y.Utils.NetUtils.FTPUtils
  10. {
  11. /// <summary>
  12. /// FTP 帮助类
  13. /// </summary>
  14. public class FtpHelper
  15. {
  16. private string ftpHostIP { get; set; }
  17. private string username { get; set; }
  18. private string password { get; set; }
  19. private string ftpURI { get { return $@"ftp://{ftpHostIP}/"; } }
  20. public FtpHelper(string ftpHostIP, string username, string password)
  21. {
  22. this.ftpHostIP = ftpHostIP;
  23. this.username = username;
  24. this.password = password;
  25. }
  26. private FtpWebRequest GetRequest(string URI)
  27. {
  28. //根据服务器信息FtpWebRequest创建类的对象
  29. FtpWebRequest result = (FtpWebRequest)WebRequest.Create(URI);
  30. result.Credentials = new NetworkCredential(username, password);
  31. result.KeepAlive = false;
  32. result.UsePassive = false;
  33. result.UseBinary = true;
  34. return result;
  35. }
  36. public bool DownloadFile(string ftpFilePath, string saveDir)
  37. {
  38. try
  39. {
  40. string filename = ftpFilePath.Substring(ftpFilePath.LastIndexOf("\\") + 1);
  41. string tmpname = Guid.NewGuid().ToString();
  42. string uri = Path.Combine(ftpURI, ftpFilePath);
  43. if (!Directory.Exists(saveDir)) Directory.CreateDirectory(saveDir);
  44. FtpWebRequest ftp = GetRequest(uri);
  45. ftp.Method = WebRequestMethods.Ftp.DownloadFile;
  46. using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
  47. {
  48. using (Stream responseStream = response.GetResponseStream())
  49. {
  50. using (FileStream fs = new FileStream(Path.Combine(saveDir, filename), FileMode.CreateNew))
  51. {
  52. byte[] buffer = new byte[2048];
  53. int read = 0;
  54. do
  55. {
  56. read = responseStream.Read(buffer, 0, buffer.Length);
  57. fs.Write(buffer, 0, read);
  58. } while (!(read == 0));
  59. fs.Flush();
  60. }
  61. }
  62. }
  63. return true;
  64. }
  65. catch { }
  66. return false;
  67. }
  68. public bool Download(string ftpFile, string localFile)
  69. {
  70. try
  71. {
  72. string localPath = DirTool.GetFilePath(localFile);
  73. if (!Directory.Exists(localPath)) Directory.CreateDirectory(localPath);
  74. string uri = Path.Combine(ftpURI, ftpFile);
  75. FtpWebRequest ftp = GetRequest(uri);
  76. ftp.Method = WebRequestMethods.Ftp.DownloadFile;
  77. using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
  78. {
  79. using (Stream responseStream = response.GetResponseStream())
  80. {
  81. using (FileStream fs = new FileStream(localFile, FileMode.CreateNew))
  82. {
  83. byte[] buffer = new byte[2048];
  84. int read = 0;
  85. do
  86. {
  87. read = responseStream.Read(buffer, 0, buffer.Length);
  88. fs.Write(buffer, 0, read);
  89. } while (!(read == 0));
  90. fs.Flush();
  91. }
  92. }
  93. }
  94. return true;
  95. }
  96. catch { }
  97. return false;
  98. }
  99. }
  100. }