FTP Client

时间:2021-06-20 15:34:20
  1:  /// <summary>
  2:     /// FTP 管理类
  3:     /// </summary>
  4:     public class FTPManage
  5:     {
  6:         private string _Host;         //服务器地址
  7:         private int? _Port;         //端口号
  8:         private string _User;         //用户名
  9:         private string _Pass;         //密码
 10:         //public string  Path;         //具体文件路径
 11:         //public string  fileName;     //具体下载文件
 12:         //public string  SaveFilePath; //保存路径
 13: 
 14:         /// <summary>
 15:         /// 构造函数
 16:         /// </summary>
 17:         /// <param name="Host"></param>
 18:         /// <param name="Port"></param>
 19:         /// <param name="User"></param>
 20:         /// <param name="Pass"></param>
 21:         public FTPManage(string Host, int? Port, string User, string Pass)
 22:         {
 23:             this._Host = Host;
 24:             this._Port = Port;
 25:             this._User = User;
 26:             this._Pass = Pass;
 27:         }
 28: 
 29:         /// <summary>
 30:         /// 判断文件是否存在
 31:         /// </summary>
 32:         /// <returns></returns>
 33:         public string ChkIsFile(string path, string fielName)
 34:         {
 35:             string defaultStr = string.Empty;                   //默认不存在
 36:             if (string.IsNullOrEmpty(fielName)) //必须填写文件名
 37:             {
 38:                 return defaultStr = "文件名不能为空";
 39:             }
 40:             string url = string.Format("ftp://{0}/{1}/{2}", _Host, path, fielName);
 41:             var request = (FtpWebRequest)WebRequest.Create(url);
 42:             request.Credentials = new NetworkCredential(_User, _Pass);
 43:             request.Method = WebRequestMethods.Ftp.GetFileSize;
 44:             try
 45:             {
 46:                 FtpWebResponse response = (FtpWebResponse)request.GetResponse();
 47:             }
 48:             catch (WebException ex)
 49:             {
 50:                 FtpWebResponse response = (FtpWebResponse)ex.Response;
 51:                 if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
 52:                 {
 53: 
 54:                 }
 55:                 return defaultStr = "获取文件异常";
 56:             }
 57:             return defaultStr;
 58:         }
 59: 
 60:         /// <summary>
 61:         /// 下载文件
 62:         /// </summary>
 63:         /// <param name="path"></param>
 64:         /// <param name="file"></param>
 65:         /// <param name="SavePath"></param>
 66:         /// <returns></returns>
 67:         public string LodeFile(string path, string fielName, string SavePath)
 68:         {
 69:             string defaultStr = string.Empty;
 70:             if (string.IsNullOrEmpty(fielName)) //必须填写文件名
 71:             {
 72:                 return defaultStr = "文件名不能为空";
 73:             }
 74:             if (string.IsNullOrEmpty(SavePath)) //必须填写文件名
 75:             {
 76:                 return defaultStr = "文件保存路径不能为空";
 77:             }
 78:             try
 79:             {
 80:                 string url = string.Format("ftp://{0}/{1}/{2}", _Host, path, fielName);
 81:                 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(url);
 82:                 reqFTP.UseBinary = true;
 83:                 reqFTP.Credentials = new NetworkCredential(_User, _Pass);
 84: 
 85:                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
 86:                 Stream ftpStream = response.GetResponseStream();
 87:                 long cl = response.ContentLength;
 88:                 int bufferSize = 2048;
 89:                 int readCount;
 90:                 byte[] buffer = new byte[bufferSize];
 91: 
 92:                 readCount = ftpStream.Read(buffer, 0, bufferSize);
 93:                 FileHelper.IsDirectory(SavePath);
 94:                 FileStream outputStream = new FileStream(SavePath + "\\" + fielName, FileMode.Create);
 95:                 while (readCount > 0)
 96:                 {
 97:                     outputStream.Write(buffer, 0, readCount);
 98:                     readCount = ftpStream.Read(buffer, 0, bufferSize);
 99:                 }
100:                 ftpStream.Close();
101:                 outputStream.Close();
102:                 response.Close();
103:             }
104:             catch (Exception ex)
105:             {
106:                 defaultStr = "文件下载异常:" + ex.Message;
107:             }
108:             return defaultStr;
109:         }
110: 
111:         /// <summary>
112:         /// 获取指定目录文件列表
113:         /// </summary>
114:         /// <returns></returns>
115:         public string[] GetFileList(string Path)
116:         {
117:             try
118:             {
119:                 string url = string.Format("ftp://{0}/{1}", _Host, Path);
120:                 //获得FTP文件上的文件列表 
121:                 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
122:                 request.Method = WebRequestMethods.Ftp.ListDirectory;
123:                 request.Credentials = new NetworkCredential(_User, _Pass);
124: 
125:                 FtpWebResponse response = (FtpWebResponse)request.GetResponse();
126:                 Stream stream = response.GetResponseStream();
127: 
128:                 StreamReader reader = new StreamReader(stream);
129:                 List<string> files = new List<string>();
130: 
131:                 while (!reader.EndOfStream)
132:                 {
133:                     string file = reader.ReadLine();
134:                     files.Add(file);
135:                 }
136: 
137:                 reader.Close();
138:                 stream.Close();
139: 
140:                 return files.ToArray();
141:             }
142:             catch { return new string[0]; }
143:         }
144: 
145:     }