获取FTP服务器上以“ipva”“ap”开头,和其他文件的数目
2017-10-16 14:21
337 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using Common; namespace SendBigData_Push { public class FtpHelper { string ftpServerIP; string ftpRemotePath; string ftpUserID; string ftpPassword; string ftpURI; string msg; int apcount = 0; int ipvacount = 0; int qicount = 0; /// <summary> /// 连接FTP /// </summary> /// <param name="FtpServerIP">FTP连接地址</param> /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param> /// <param name="FtpUserID">用户名</param> /// <param name="FtpPassword">密码</param> public FtpHelper(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword) { ftpServerIP = FtpServerIP; ftpRemotePath = FtpRemotePath; ftpUserID = FtpUserID; ftpPassword = FtpPassword; ftpURI = "ftp://" + ftpServerIP + "/"; } /// <summary> /// 获取当前目录下所有的文件夹列表(仅文件夹) /// </summary> /// <returns></returns> public string GetDirectoryRoot(string url) { apcount = 0; ipvacount = 0; qicount = 0; string[] drectory = GetFilesDetailList(url); string m = string.Empty; foreach (string str in drectory) { if (str != "") { int dirPos = str.IndexOf("<DIR>"); if (dirPos > 0) { /*判断 Windows 风格*/ m += str.Substring(dirPos + 5).Trim() + "\n"; } else if (str.Trim().Substring(0, 1).ToUpper() == "D") { /*判断 Unix 风格*/ string dir = str.Substring(55).Trim(); if (dir != "." && dir != "..") { m += dir + "\n"; } } else { if (str.Substring(55).Trim().Substring(0, 2).ToLower() == "ap") { 4000 apcount++; } else if (str.Substring(55).Trim().Substring(0, 4).ToLower() == "ipva") { ipvacount++; } else { qicount++; } } } } //WLog.WriteLog("SendBigData_Push", url + "下ap文件个数是:" + apcount.ToString() + ";ipva文件个数是:" + ipvacount.ToString() + "其他文件个数是:" + qicount.ToString()); char[] n = new char[] { '\n' }; string[] drectorys = m.Split(n); foreach (string f in drectorys) { url = ftpURI; if (f != "") { url = url + f + "/"; msg = msg + "。" + GetDirectoryList(url); } } return "FTP服务器以ipva开头文件有" + ipvacount.ToString() + "个,ap开头文件有" + apcount.ToString() + "个,其他文件有" + qicount.ToString() + "个。"; } /// <summary> /// 获取当前目录下所有的文件夹列表(仅文件夹) /// </summary> /// <returns></returns> public string GetDirectoryList(string url) { string[] drectory = GetFilesDetailList(url); string m = string.Empty; foreach (string str in drectory) { if (str != "") { int dirPos = str.IndexOf("<DIR>"); if (dirPos > 0) { /*判断 Windows 风格*/ m += str.Substring(dirPos + 5).Trim() + "\n"; } else if (str.Trim().Substring(0, 1).ToUpper() == "D") { /*判断 Unix 风格*/ string dir = str.Substring(55).Trim(); if (dir != "." && dir != "..") { m += dir + "\n"; } } else { if (str.Substring(55).Trim().Substring(0, 2).ToLower() == "ap") { apcount++; } else if (str.Substring(55).Trim().Substring(0, 4).ToLower() == "ipva") { ipvacount++; } else { qicount++; } } } } //WLog.WriteLog("SendBigData_Push", url + "下ap文件个数是:" + apcount.ToString() + ";ipva文件个数是:" + ipvacount.ToString() + "其他文件个数是:" + qicount.ToString()); char[] n = new char[] { '\n' }; string[] drectorys = m.Split(n); foreach (string f in drectorys) { if (f != "") { url = url + f + "/"; msg = msg + "。" + GetDirectoryList(url); } } return url + "下ap文件个数是:" + apcount.ToString() + ";ipva文件个数是:" + ipvacount.ToString() + "其他文件个数是:" + qicount.ToString(); } /// <summary> /// 获取当前目录下明细(包含文件和文件夹) /// </summary> /// <returns></returns> public string[] GetFilesDetailList(string url) { string[] downloadFiles; try { StringBuilder result = new StringBuilder(); FtpWebRequest ftp; ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url)); ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword); ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails; ftp.UsePassive = false; WebResponse response = ftp.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default); string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append("\n"); line = reader.ReadLine(); } if (result.Length > 2) result.Remove(result.ToString().LastIndexOf("\n"), 1); reader.Close(); response.Close(); return result.ToString().Split('\n'); } catch (Exception ex) { downloadFiles = null; throw new Exception("FtpHelper Error --> " + ex.Message); } } } } 调用方法 /// <summary> /// 获取Ftp信息方法 /// </summary> public void GetFtpMessage() { string FtpIP="192.168.2.230:7210"; string FtpUser="test"; string FtpPassword="test"; FtpHelper ftp = new FtpHelper(cf.FtpIP, null, cf.FtpUser, cf.FtpPassword); string describe = ftp.GetDirectoryRoot("ftp://" + cf.FtpIP + "/"); }
相关文章推荐
- FTP服务器LIST指令获取的文件列表信息解析(UNIX格式)
- 使用Apache 的 FTP Client获取FTP服务器上的文件列表FTP response 421 received.Server closed connection问题的解决
- php 一键上传文件,ftp上传和下载到其他服务器
- Android从ftp服务器获取文件
- JAVA+FTP实现跨服务器获取文件,支持局域网和外网
- php获取svn文件,然后ftp上传服务器代码
- 获取ftp服务器文件和目录列表
- FTP服务器文件的上传,下载和获取
- linux 自动获取FTP服务器文件shell
- FTP服务器文件操作(上传/下载/删除/创建/获取)
- Ubuntu下从ftp服务器获取文件的方法
- 从ftp服务器获取文件
- python 实现统计ftp服务器指定目录下文件夹数目、文件数目及所有文件大小 本次主要为满足应用方核对上传到ftp服务器的文件是否缺漏。 主要要求:指定目录下,文件夹数目/文件数目/所有文件大小
- centos6.4下定时通过ftp获取数据库服务器上的文件并且删除远程目录里面的文件
- 使用Apache 的 FTP Client获取FTP服务器上的文件列表FTP response 421 received.Server closed connection问题的解决
- 获取ftp服务器上指定文件夹的文件列表(包含文件大小)
- 利用WinInet库和STL获取计算机硬盘中的文件并上传到指定FTP服务器
- 利用WinInet库和STL获取计算机硬盘中的文件并上传到指定FTP服务器
- 监听FTP服务器来获取读写完成的文件
- FTP接口上传文件到对方服务器