Ftp实用类

  1 public class FtpLib
  2     {
  3         string ftpServerIP;
  4         string ftpUserID;
  5         string ftpPassword;
  6         FtpWebRequest reqFTP;
  7 
  8         public FtpLib(string ftpServerIP, string ftpUserID, string ftpPassword)
  9         {
 10             this.ftpServerIP = ftpServerIP;
 11             this.ftpUserID = ftpUserID;
 12             this.ftpPassword = ftpPassword;
 13         }
 14 
 15         /// <summary>
 16         /// 创建连接
 17         /// </summary>
 18         /// <param name="path"></param>
 19         private void Connect(String path)
 20         {
 21             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
 22             reqFTP.UseBinary = true;
 23             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
 24         }
 25 
 26         /// <summary>
 27         /// 获取文件列表
 28         /// </summary>
 29         /// <param name="path"></param>
 30         /// <param name="WRMethods"></param>
 31         /// <returns></returns>
 32         private string[] GetFileList(string path, string WRMethods)
 33         {
 34             string[] downloadFiles;
 35             StringBuilder result = new StringBuilder();
 36             try
 37             {
 38                 Connect(path);
 39                 reqFTP.Method = WRMethods;
 40                 WebResponse response = reqFTP.GetResponse();
 41                 StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名
 42                 string line = reader.ReadLine();
 43                 while (line != null)
 44                 {
 45                     result.Append(line);
 46                     result.Append("
");
 47                     line = reader.ReadLine();
 48                 }
 49                 // to remove the trailing '
'
 50                 result.Remove(result.ToString().LastIndexOf('
'), 1);
 51                 reader.Close();
 52                 response.Close();
 53                 return result.ToString().Split('
');
 54             }
 55             catch (Exception ex)
 56             {
 57                 Helper.WriteLog(ex.Message + "
" + ex.StackTrace);
 58                 downloadFiles = null;
 59                 return downloadFiles;
 60             }
 61         }
 62 
 63         /// <summary>
 64         /// 上传文件
 65         /// </summary>
 66         /// <param name="filename"></param>
 67         public void Upload(string filename)
 68         {
 69             FileInfo fileInf = new FileInfo(filename);
 70             string uri = "ftp://" + ftpServerIP + "/" +DateTime.Now.ToString("yyyyMMdd")+ "/" + fileInf.Name;
 71             Connect(uri);
 72             reqFTP.KeepAlive = false;
 73             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
 74             reqFTP.ContentLength = fileInf.Length;
 75             int buffLength = 2048;
 76             byte[] buff = new byte[buffLength];
 77             int contentLen;
 78             FileStream fs = fileInf.OpenRead();
 79             try
 80             {
 81                 Stream strm = reqFTP.GetRequestStream();
 82                 contentLen = fs.Read(buff, 0, buffLength);
 83                 while (contentLen != 0)
 84                 {
 85                     strm.Write(buff, 0, contentLen);
 86                     contentLen = fs.Read(buff, 0, buffLength);
 87                 }
 88                 strm.Close();
 89                 fs.Close();
 90             }
 91             catch (Exception ex)
 92             {
 93                 Helper.WriteLog(ex.Message + "
" + ex.StackTrace);
 94             }
 95         }
 96 
 97         /// <summary>
 98         /// 删除文件
 99         /// </summary>
100         /// <param name="fileName"></param>
101         public void DeleteFileName(string fileName)
102         {
103             try
104             {
105                 FileInfo fileInf = new FileInfo(fileName);
106                 string uri = "ftp://" + ftpServerIP + "/" + DateTime.Now.ToString("yyyyMMdd") + "/" + fileInf.Name;
107                 Connect(uri);
108                 reqFTP.KeepAlive = false;
109                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
110                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
111                 response.Close();
112             }
113             catch (Exception ex)
114             {
115                 Helper.WriteLog(ex.Message + "
" + ex.StackTrace);
116             }
117         }
118 
119         /// <summary>
120         /// 判断文件是否存在
121         /// </summary>
122         /// <param name="fileName"></param>
123         /// <param name="ftpPath"></param>
124         /// <returns></returns>
125         public bool ExecFile(string fileName, string ftpPath)
126         {
127 
128             FtpCheckDirectoryExist(ftpPath);
129 
130             string[] strList = GetFileList(ftpPath, WebRequestMethods.Ftp.ListDirectoryDetails);
131             if (strList != null)
132             {
133                 if (strList.Length > 0)
134                 {
135                     foreach (string str in strList)
136                     {
137                         if (str.Contains(fileName))
138                         {
139                             return true;
140                         }
141                     }
142                 }
143             }
144             else
145             {
146 
147             }
148             return false;
149         }
150 
151         //判断文件夹是否存,不存则创建,存在则返回 
152         public void FtpCheckDirectoryExist(string destFilePath)
153         {
154             string fullDir = FtpParseDirectory(destFilePath);
155             string[] dirs = fullDir.Split('/');
156             string curDir = DateTime.Now.ToString("yyyyMMdd");
157             for (int i = 0; i < dirs.Length; i++)
158             {
159                 string dir = dirs[i];
160                 if (dir == string.Empty)
161                 {
162                     try
163                     {
164                         curDir += dir + "/";
165                         FtpMakeDir(curDir);
166                         break;
167                     }
168                     catch (Exception)
169                     { }
170                 }
171             }
172         }
173         public string FtpParseDirectory(string destFilePath)
174         {
175             return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
176         }
177         //创建目录  
178         public Boolean FtpMakeDir(string localFile)
179         {
180             FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + this.ftpServerIP + "/" + localFile);
181             req.Credentials = new NetworkCredential(this.ftpUserID, this.ftpPassword);
182             req.Method = WebRequestMethods.Ftp.MakeDirectory;
183             try
184             {
185                 FtpWebResponse response = (FtpWebResponse)req.GetResponse();
186                 response.Close();
187             }
188             catch (Exception)
189             {
190                 req.Abort();
191                 return false;
192             }
193             req.Abort();
194             return true;
195         }
196     }
原文地址:https://www.cnblogs.com/jeffqing/p/3236755.html