ftpHelper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace TrainVideoDataDispose
  9. {
  10. public class ftpHelper
  11. {
  12. /// <summary>
  13. /// 获取当前目录下文件夹
  14. /// </summary>
  15. /// <returns></returns>
  16. public static string[] GetFilesDirList()
  17. {
  18. try
  19. {
  20. List<string> result = new List<string>();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。
  21. List<string> resultTxt = new List<string>();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。
  22. List<string> resultJpg = new List<string>();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。
  23. List<string> resultMp4 = new List<string>();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。
  24. FtpWebRequest ftp;
  25. ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://10.111.16.113"));//"ftp://10.12.12.9";
  26. ftp.Credentials = new NetworkCredential("300t", "300t");
  27. ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;//目录
  28. WebResponse response = ftp.GetResponse();//response为一个ftp的WebResponse
  29. StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);//读入responses所创建的数据流
  30. string line = reader.ReadLine();//输入流中的下一行;如果到达了输入流的末尾,则为空引用
  31. while (line != null)
  32. {
  33. result.Add(line);//)Append 方法可用来将文本或对象的字符串表示形式添加到由当前 StringBuilder 对象表示的字符串的结尾处。
  34. line = reader.ReadLine();
  35. //Download();
  36. }
  37. foreach (var item in result)
  38. {
  39. if (item.Contains(".txt"))
  40. {
  41. resultTxt.Add(item);
  42. }
  43. }
  44. foreach (var item in result)
  45. {
  46. if (item.Contains(".jpeg"))
  47. {
  48. resultJpg.Add(item);
  49. }
  50. }
  51. foreach (var item in result)
  52. {
  53. if (item.Contains(".mp4"))
  54. {
  55. resultMp4.Add(item);
  56. }
  57. }
  58. string pathImg = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/img";
  59. string pathTxt = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/txt";
  60. string pathMp4 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/movie";
  61. if (!Directory.Exists(pathImg))
  62. {
  63. Directory.CreateDirectory(pathImg);
  64. }
  65. if (!Directory.Exists(pathTxt))
  66. {
  67. Directory.CreateDirectory(pathTxt);
  68. }
  69. if (!Directory.Exists(pathMp4))
  70. {
  71. Directory.CreateDirectory(pathMp4);
  72. }
  73. foreach (var item in resultTxt)
  74. {
  75. string fileName = item.Substring(item.Length - 23);
  76. Download(pathTxt, fileName);
  77. }
  78. //foreach (var item in resultMp4)
  79. //{
  80. // string fileName = item.Substring(item.Length - 23);
  81. // Download(pathMp4, fileName);
  82. //}
  83. //foreach (var item in resultJpg)
  84. //{
  85. // string fileName = item.Substring(item.Length - 26);
  86. // Download(pathImg, fileName);
  87. //}
  88. reader.Close();
  89. response.Close();
  90. return result.ToString().Split('\n');
  91. }
  92. catch (Exception ex)
  93. {
  94. throw ex;
  95. }
  96. }
  97. /// <summary>
  98. /// 下载 filePath是下载到本机的地址fileName是需要下载的文件的名字
  99. /// </summary>
  100. public static void Download(string filePath, string fileName)
  101. {
  102. try
  103. {
  104. string path = filePath + "\\" + fileName;
  105. FileStream outputStream = new FileStream(path, FileMode.Create);
  106. FtpWebRequest reqFTP;
  107. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://10.111.16.113/" + fileName));
  108. reqFTP.Credentials = new NetworkCredential("300t", "300t");
  109. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  110. reqFTP.UseBinary = true;
  111. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  112. Stream ftpStream = response.GetResponseStream();
  113. long cl = response.ContentLength;
  114. int bufferSize = 2048;
  115. int readCount;
  116. byte[] buffer = new byte[bufferSize];
  117. readCount = ftpStream.Read(buffer, 0, bufferSize);
  118. while (readCount > 0)
  119. {
  120. outputStream.Write(buffer, 0, readCount);
  121. readCount = ftpStream.Read(buffer, 0, bufferSize);
  122. }
  123. ftpStream.Close();
  124. outputStream.Close();
  125. response.Close();
  126. }
  127. catch (Exception ex)
  128. {
  129. throw new Exception(ex.Message);
  130. }
  131. }
  132. }
  133. }