FluentFTPClient.cs 4.9 KB

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