FtpUploaderTest.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using FluentFTP;
  2. using System;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace TeamAAS_VP.Core
  9. {
  10. public class FtpUploaderTest
  11. {
  12. public FtpUploaderTest()
  13. {
  14. }
  15. public async void UploadFileTest()
  16. {
  17. //var rootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CuttingLine");
  18. //string sourcePath = "Path.Combine(rootPath, SelectedFile)";
  19. string sourcePath = @"C:\Users\Administrator\Desktop\test.txt";
  20. string content = File.ReadAllText(sourcePath);
  21. string ftpHost = "192.168.0.40";
  22. string ftpUserName = "default";
  23. string ftpPwd = "default";
  24. content += "END";
  25. var byteArray = Encoding.ASCII.GetBytes(content);
  26. // FluentFTP async upload (replaces deprecated FtpWebRequest)
  27. using (var ftp = new FtpClient(ftpHost, new NetworkCredential(ftpUserName, ftpPwd)))
  28. {
  29. ftp.Config.EncryptionMode = FtpEncryptionMode.None; // plain FTP
  30. ftp.Config.DataConnectionType = FtpDataConnectionType.PASV; // passive mode
  31. ftp.Connect();
  32. var remotePath = "/usr/usrapp/saveCAM/data.txt";
  33. using (var ms = new MemoryStream(byteArray))
  34. {
  35. ftp.UploadStream(
  36. ms,
  37. remotePath,
  38. FtpRemoteExists.Overwrite,
  39. createRemoteDir: true
  40. );
  41. await Task.Delay(100).ConfigureAwait(false);
  42. Console.WriteLine("File uploaded successfully.");
  43. }
  44. }
  45. }
  46. // 测试上传ZIP文件,保持内容不变
  47. public async Task UploadZipFileTest()
  48. {
  49. string sourcePath = @"C:\Users\Administrator\Desktop\test.zip"; // ZIP文件路径
  50. string ftpHost = "192.168.0.40";
  51. string ftpUserName = "default";
  52. string ftpPwd = "default";
  53. // 读取文件字节(不修改内容)
  54. byte[] fileBytes = File.ReadAllBytes(sourcePath);
  55. using (var ftp = new FtpClient(ftpHost, new NetworkCredential(ftpUserName, ftpPwd)))
  56. {
  57. ftp.Config.EncryptionMode = FtpEncryptionMode.None;
  58. ftp.Config.DataConnectionType = FtpDataConnectionType.PASV;
  59. ftp.Connect();
  60. var remotePath = "/usr/usrapp/saveCAM/data.zip"; // 远程路径
  61. using (var ms = new MemoryStream(fileBytes))
  62. {
  63. ftp.UploadStream(
  64. ms,
  65. remotePath,
  66. FtpRemoteExists.Overwrite,
  67. createRemoteDir: true
  68. );
  69. Console.WriteLine("ZIP file uploaded successfully.");
  70. }
  71. }
  72. }
  73. // 测试上传整个文件夹,保持结构不变
  74. public async Task UploadFolderTest()
  75. {
  76. string localFolderPath = @"C:\Users\Administrator\Desktop\MyFolder"; // 本地文件夹
  77. string ftpHost = "192.168.0.40";
  78. string ftpUserName = "default";
  79. string ftpPwd = "default";
  80. string remoteBasePath = "/usr/usrapp/saveCAM/MyFolder"; // 远程基础路径
  81. using (var ftp = new FtpClient(ftpHost, new NetworkCredential(ftpUserName, ftpPwd)))
  82. {
  83. ftp.Config.EncryptionMode = FtpEncryptionMode.None;
  84. ftp.Config.DataConnectionType = FtpDataConnectionType.PASV;
  85. ftp.Connect();
  86. await UploadDirectoryRecursively(ftp, localFolderPath, remoteBasePath);
  87. Console.WriteLine("Folder uploaded successfully.");
  88. }
  89. }
  90. private async Task UploadDirectoryRecursively(FtpClient ftp, string localPath, string remotePath)
  91. {
  92. // 创建远程目录
  93. if (!ftp.DirectoryExists(remotePath))
  94. {
  95. ftp.CreateDirectory(remotePath);
  96. }
  97. // 上传文件
  98. foreach (string file in Directory.GetFiles(localPath))
  99. {
  100. string fileName = Path.GetFileName(file);
  101. string remoteFilePath = $"{remotePath}/{fileName}".Replace("\\", "/");
  102. byte[] fileBytes = File.ReadAllBytes(file);
  103. using (var ms = new MemoryStream(fileBytes))
  104. {
  105. ftp.UploadStream(
  106. ms,
  107. remoteFilePath,
  108. FtpRemoteExists.Overwrite,
  109. createRemoteDir: false
  110. );
  111. }
  112. Console.WriteLine($"Uploaded file: {fileName}");
  113. }
  114. // 递归处理子文件夹
  115. foreach (string subDir in Directory.GetDirectories(localPath))
  116. {
  117. string dirName = new DirectoryInfo(subDir).Name;
  118. string remoteSubPath = $"{remotePath}/{dirName}".Replace("\\", "/");
  119. await UploadDirectoryRecursively(ftp, subDir, remoteSubPath);
  120. }
  121. await Task.CompletedTask;
  122. }
  123. // 测试上传整个文件夹作为ZIP,保持内容不变
  124. public async Task UploadFolderAsZipTest()
  125. {
  126. string sourceFolder = @"D:\image\Recored\2026-03\03-02\产品1\CN0D0NDYCKJ006250103X02";
  127. string tempZipPath = Path.Combine(Path.GetTempPath(), $"CN0D0NDYCKJ006250103X02.zip");
  128. try
  129. {
  130. // 创建ZIP文件
  131. ZipFile.CreateFromDirectory(sourceFolder, tempZipPath);
  132. // 上传ZIP
  133. string ftpHost = "10.130.63.3";
  134. string ftpUserName = "AE";
  135. string ftpPwd = "LUX#PCiCT2026";
  136. byte[] zipBytes = File.ReadAllBytes(tempZipPath);
  137. using (var ftp = new FtpClient(ftpHost, new NetworkCredential(ftpUserName, ftpPwd)))
  138. {
  139. ftp.Config.EncryptionMode = FtpEncryptionMode.None;
  140. ftp.Config.DataConnectionType = FtpDataConnectionType.PASV;
  141. ftp.Config.ConnectTimeout = 15000;
  142. ftp.Connect();
  143. var remotePath = $"/ae/KB_Screw_1/CN0D0NDYCKJ006250103X02.zip";
  144. using (var ms = new MemoryStream(zipBytes))
  145. {
  146. ftp.UploadStream(
  147. ms,
  148. remotePath,
  149. FtpRemoteExists.Overwrite,
  150. createRemoteDir: true
  151. );
  152. }
  153. Console.WriteLine("Folder compressed and uploaded as ZIP successfully.");
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158. System.Windows.Forms.MessageBox.Show(ex.Message);
  159. }
  160. finally
  161. {
  162. // 清理临时文件
  163. if (File.Exists(tempZipPath))
  164. {
  165. File.Delete(tempZipPath);
  166. }
  167. }
  168. }
  169. }
  170. }