FluentFTPClient.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using FluentFTP;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Authentication;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace TestTool.RemoteTriggerAPP
  10. {
  11. internal class FluentFTPClient
  12. {
  13. #region 属性与构造函数
  14. /// <summary>
  15. /// IP地址
  16. /// </summary>
  17. public string IpAddr { get; set; }
  18. /// <summary>
  19. /// 相对路径
  20. /// </summary>
  21. public string RelatePath { get; set; }
  22. /// <summary>
  23. /// 端口号
  24. /// </summary>
  25. public int Port { get; set; }
  26. /// <summary>
  27. /// 用户名
  28. /// </summary>
  29. public string UserName { get; set; }
  30. /// <summary>
  31. /// 密码
  32. /// </summary>
  33. public string Password { get; set; }
  34. public FluentFTPClient(string ipAddr, int port, string userName, string password, string relatePath)
  35. {
  36. this.IpAddr = ipAddr;
  37. this.Port = port;
  38. this.UserName = userName;
  39. this.Password = password;
  40. this.RelatePath = relatePath;
  41. }
  42. #endregion
  43. #region 方法
  44. public FtpListItem[] ListDir()
  45. {
  46. FtpListItem[] lists;
  47. using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
  48. {
  49. client.Config.EncryptionMode = FtpEncryptionMode.Explicit;
  50. client.Config.SslProtocols = SslProtocols.Tls12;
  51. client.ValidateCertificate += Client_ValidateCertificate;
  52. client.Connect();
  53. client.SetWorkingDirectory(this.RelatePath);
  54. lists = client.GetListing();
  55. }
  56. return lists;
  57. }
  58. private void Client_ValidateCertificate(FluentFTP.Client.BaseClient.BaseFtpClient control, FtpSslValidationEventArgs e)
  59. {
  60. if (e.PolicyErrors != System.Net.Security.SslPolicyErrors.None)
  61. {
  62. e.Accept = false;
  63. }
  64. else
  65. {
  66. e.Accept = true;
  67. }
  68. }
  69. async internal Task<bool> UploadFileAsync(string localFilePath, string remoteFilePath)
  70. {
  71. bool isOk = false;
  72. using (var client = new AsyncFtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
  73. {
  74. client.Config.EncryptionMode = FtpEncryptionMode.Implicit;
  75. client.Config.SslProtocols = SslProtocols.Tls12;
  76. client.ValidateCertificate += Client_ValidateCertificate;
  77. await client.SetWorkingDirectory(this.RelatePath);
  78. await client.Connect();
  79. var status = await client.UploadFile(localFilePath, "/" + remoteFilePath, FtpRemoteExists.Overwrite, false, FtpVerify.Retry);
  80. isOk = status == FtpStatus.Success;
  81. }
  82. return isOk;
  83. }
  84. public bool Upload(string dir, string file)
  85. {
  86. bool isOk = false;
  87. FileInfo fi = new FileInfo(Path.Combine(dir, file));
  88. using (FileStream fs = fi.OpenRead())
  89. {
  90. //long length = fs.Length;
  91. using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
  92. {
  93. client.Config.EncryptionMode = FtpEncryptionMode.Implicit;
  94. client.Config.SslProtocols = SslProtocols.Tls12;
  95. client.ValidateCertificate += Client_ValidateCertificate;
  96. client.Connect();
  97. client.SetWorkingDirectory(this.RelatePath);
  98. string remotePath = Path.GetFileName(file);
  99. FtpStatus status = client.UploadStream(fs, remotePath, FtpRemoteExists.Overwrite, true);
  100. isOk = status == FtpStatus.Success;
  101. }
  102. }
  103. return isOk;
  104. }
  105. /// <summary>
  106. /// 下载ftp
  107. /// </summary>
  108. /// <param name="localAddress"></param>
  109. /// <param name="remoteAddress"></param>
  110. /// <returns></returns>
  111. public bool DownloadFile(string localAddress, string remoteAddress)
  112. {
  113. using (var client = new FtpClient(this.IpAddr, this.UserName, this.Password, this.Port))
  114. {
  115. client.Config.EncryptionMode = FtpEncryptionMode.Explicit;
  116. client.Config.SslProtocols = SslProtocols.Tls12;
  117. client.ValidateCertificate += Client_ValidateCertificate;
  118. client.SetWorkingDirectory("/");
  119. client.Connect();
  120. if (client.DownloadFile(localAddress, remoteAddress) == FtpStatus.Success)
  121. {
  122. return true;
  123. }
  124. return false;
  125. }
  126. }
  127. #endregion
  128. }
  129. }