| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace TrainVideoDataDispose
- {
- public class ftpHelper
- {
- /// <summary>
- /// 获取当前目录下文件夹
- /// </summary>
- /// <returns></returns>
- public static string[] GetFilesDirList()
- {
- try
- {
- List<string> result = new List<string>();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。
- List<string> resultTxt = new List<string>();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。
- List<string> resultJpg = new List<string>();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。
- List<string> resultMp4 = new List<string>();//如果要修改字符串而不创建新的对象,则可以使用 System.Text.StringBuilder 类。
- FtpWebRequest ftp;
- ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://10.111.16.113"));//"ftp://10.12.12.9";
- ftp.Credentials = new NetworkCredential("300t", "300t");
- ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;//目录
- WebResponse response = ftp.GetResponse();//response为一个ftp的WebResponse
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);//读入responses所创建的数据流
- string line = reader.ReadLine();//输入流中的下一行;如果到达了输入流的末尾,则为空引用
- while (line != null)
- {
- result.Add(line);//)Append 方法可用来将文本或对象的字符串表示形式添加到由当前 StringBuilder 对象表示的字符串的结尾处。
- line = reader.ReadLine();
- //Download();
- }
- foreach (var item in result)
- {
- if (item.Contains(".txt"))
- {
- resultTxt.Add(item);
- }
- }
- foreach (var item in result)
- {
- if (item.Contains(".jpeg"))
- {
- resultJpg.Add(item);
- }
- }
- foreach (var item in result)
- {
- if (item.Contains(".mp4"))
- {
- resultMp4.Add(item);
- }
- }
- string pathImg = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/img";
- string pathTxt = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/txt";
- string pathMp4 = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/movie";
- if (!Directory.Exists(pathImg))
- {
- Directory.CreateDirectory(pathImg);
- }
- if (!Directory.Exists(pathTxt))
- {
- Directory.CreateDirectory(pathTxt);
- }
- if (!Directory.Exists(pathMp4))
- {
- Directory.CreateDirectory(pathMp4);
- }
- foreach (var item in resultTxt)
- {
- string fileName = item.Substring(item.Length - 23);
- Download(pathTxt, fileName);
- }
- //foreach (var item in resultMp4)
- //{
- // string fileName = item.Substring(item.Length - 23);
- // Download(pathMp4, fileName);
- //}
- //foreach (var item in resultJpg)
- //{
- // string fileName = item.Substring(item.Length - 26);
- // Download(pathImg, fileName);
- //}
- reader.Close();
- response.Close();
- return result.ToString().Split('\n');
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 下载 filePath是下载到本机的地址fileName是需要下载的文件的名字
- /// </summary>
- public static void Download(string filePath, string fileName)
- {
- try
- {
- string path = filePath + "\\" + fileName;
- FileStream outputStream = new FileStream(path, FileMode.Create);
- FtpWebRequest reqFTP;
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://10.111.16.113/" + fileName));
- reqFTP.Credentials = new NetworkCredential("300t", "300t");
- reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
- reqFTP.UseBinary = true;
- FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
- Stream ftpStream = response.GetResponseStream();
- long cl = response.ContentLength;
- int bufferSize = 2048;
- int readCount;
- byte[] buffer = new byte[bufferSize];
- readCount = ftpStream.Read(buffer, 0, bufferSize);
- while (readCount > 0)
- {
- outputStream.Write(buffer, 0, readCount);
- readCount = ftpStream.Read(buffer, 0, bufferSize);
- }
- ftpStream.Close();
- outputStream.Close();
- response.Close();
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- }
- }
|