FTP文件操作
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.IO; 7 using System.Net; 8 9 namespace ConsoleApplication1 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //FTPUploadFile("127.0.0.1/zwx" , "administrator", "123123", "e:\饭卡记录清单.xls"); 16 //FTPDownloadFile("127.0.0.1/zwx", "administrator", "123123", "d:", "123.xls", "饭卡记录清单.xls"); 17 string[] fileList = FTPGetFileList("127.0.0.1/zwx/1", "administrator", "123123"); 18 for (int i = 0; i < fileList.Length; i++) 19 { 20 Console.WriteLine(fileList[i]); 21 } 22 23 Console.Read(); 24 } 25 26 #region FTP获取文件列表 27 28 /// <summary> 29 /// FTP获取文件列表 30 /// </summary> 31 /// <param name="ftpServerIP"></param> 32 /// <param name="ftpUserID"></param> 33 /// <param name="ftpPassword"></param> 34 /// <returns></returns> 35 private static string[] FTPGetFileList(string ftpServerIP, string ftpUserID, string ftpPassword) 36 { 37 //响应结果 38 StringBuilder result = new StringBuilder(); 39 40 //FTP请求 41 FtpWebRequest ftpRequest = null; 42 43 //FTP响应 44 WebResponse ftpResponse = null; 45 46 //FTP响应流 47 StreamReader ftpResponsStream = null; 48 49 try 50 { 51 //生成FTP请求 52 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/")); 53 54 //设置文件传输类型 55 ftpRequest.UseBinary = true; 56 57 //FTP登录 58 ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 59 60 //设置FTP方法 61 ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; 62 63 //生成FTP响应 64 ftpResponse = ftpRequest.GetResponse(); 65 66 //FTP响应流 67 ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream()); 68 69 string line = ftpResponsStream.ReadLine(); 70 71 while (line != null) 72 { 73 result.Append(line); 74 result.Append(" "); 75 line = ftpResponsStream.ReadLine(); 76 } 77 78 //去掉结果列表中最后一个换行 79 result.Remove(result.ToString().LastIndexOf(' '), 1); 80 81 //返回结果 82 return result.ToString().Split(' '); 83 } 84 catch (Exception ex) 85 { 86 Console.WriteLine(ex.Message); 87 return (null); 88 } 89 finally 90 { 91 if (ftpResponsStream != null) 92 { 93 ftpResponsStream.Close(); 94 } 95 96 if (ftpResponse != null) 97 { 98 ftpResponse.Close(); 99 } 100 } 101 } 102 103 #endregion 104 105 #region FTP下载文件 106 107 /// <summary> 108 /// FTP下载文件 109 /// </summary> 110 /// <param name="ftpServerIP">FTP服务器IP</param> 111 /// <param name="ftpUserID">FTP登录帐号</param> 112 /// <param name="ftpPassword">FTP登录密码</param> 113 /// <param name="saveFilePath">保存文件路径</param> 114 /// <param name="saveFileName">保存文件名</param> 115 /// <param name="downloadFileName">下载文件名</param> 116 private static void FTPDownloadFile(string ftpServerIP, string ftpUserID, string ftpPassword, 117 string saveFilePath, string saveFileName, string downloadFileName) 118 { 119 //定义FTP请求对象 120 FtpWebRequest ftpRequest = null; 121 //定义FTP响应对象 122 FtpWebResponse ftpResponse = null; 123 124 //存储流 125 FileStream saveStream = null; 126 //FTP数据流 127 Stream ftpStream = null; 128 129 try 130 { 131 //生成下载文件 132 saveStream = new FileStream(saveFilePath + "\" + saveFileName, FileMode.Create); 133 134 //生成FTP请求对象 135 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + downloadFileName)); 136 137 //设置下载文件方法 138 ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; 139 140 //设置文件传输类型 141 ftpRequest.UseBinary = true; 142 143 //设置登录FTP帐号和密码 144 ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 145 146 //生成FTP响应对象 147 ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 148 149 //获取FTP响应流对象 150 ftpStream = ftpResponse.GetResponseStream(); 151 152 //响应数据长度 153 long cl = ftpResponse.ContentLength; 154 155 int bufferSize = 2048; 156 157 int readCount; 158 159 byte[] buffer = new byte[bufferSize]; 160 161 //接收FTP文件流 162 readCount = ftpStream.Read(buffer, 0, bufferSize); 163 164 while (readCount > 0) 165 { 166 saveStream.Write(buffer, 0, readCount); 167 168 readCount = ftpStream.Read(buffer, 0, bufferSize); 169 } 170 171 } 172 catch (Exception ex) 173 { 174 Console.WriteLine(ex.Message); 175 } 176 finally 177 { 178 if (ftpStream != null) 179 { 180 ftpStream.Close(); 181 } 182 183 if (saveStream != null) 184 { 185 saveStream.Close(); 186 } 187 188 if (ftpResponse != null) 189 { 190 ftpResponse.Close(); 191 } 192 } 193 } 194 195 #endregion 196 197 #region FTP上传文件 198 199 /// <summary> 200 /// FTP上传文件 201 /// </summary> 202 /// <param name="ftpServerIP">FTP服务器IP</param> 203 /// <param name="ftpUserID">FTP登录帐号</param> 204 /// <param name="ftpPassword">FTP登录密码</param> 205 /// <param name="filename">上文件文件名(绝对路径)</param> 206 private static void FTPUploadFile(string ftpServerIP, string ftpUserID, string ftpPassword, string filename) 207 { 208 //上传文件 209 FileInfo uploadFile = null; 210 211 //上传文件流 212 FileStream uploadFileStream = null; 213 214 //FTP请求对象 215 FtpWebRequest ftpRequest = null; 216 217 //FTP流 218 Stream ftpStream = null; 219 220 try 221 { 222 //获取上传文件 223 uploadFile = new FileInfo(filename); 224 225 //创建FtpWebRequest对象 226 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + uploadFile.Name)); 227 228 //FTP登录 229 ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 230 231 // 默认为true,连接不会被关闭 232 // 在一个命令之后被执行 233 ftpRequest.KeepAlive = false; 234 235 //FTP请求执行方法 236 ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; 237 238 // 指定数据传输类型 239 ftpRequest.UseBinary = true; 240 241 // 上传文件时通知服务器文件的大小 242 ftpRequest.ContentLength = uploadFile.Length; 243 244 // 缓冲大小设置为2kb 245 int buffLength = 2048; 246 247 byte[] buff = new byte[buffLength]; 248 int contentLen; 249 250 // 打开一个文件流读上传的文件 251 uploadFileStream = uploadFile.OpenRead(); 252 253 // 把上传的文件写入流 254 ftpStream = ftpRequest.GetRequestStream(); 255 256 // 每次读文件流的2kb 257 contentLen = uploadFileStream.Read(buff, 0, buffLength); 258 259 // 流内容没有结束 260 while (contentLen != 0) 261 { 262 // 把内容从file stream 写入 upload stream 263 ftpStream.Write(buff, 0, contentLen); 264 265 contentLen = uploadFileStream.Read(buff, 0, buffLength); 266 } 267 268 } 269 catch (Exception ex) 270 { 271 Console.WriteLine(ex.Message); 272 } 273 finally 274 { 275 if (uploadFileStream != null) 276 { 277 uploadFileStream.Close(); 278 } 279 280 if (ftpStream != null) 281 { 282 ftpStream.Close(); 283 } 284 } 285 } 286 287 #endregion 288 } 289 }