1. <pre class="csharp" name="code"><pre class="csharp" name="code">using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. namespace JianKunKing.Common.Ftp
  8. {
  9. /// <summary>
  10. /// ftp方式文件下载上传
  11. /// </summary>
  12. public static class FileUpDownload
  13. {
  14. #region 变量属性
  15. /// <summary>
  16. /// Ftp服务器ip
  17. /// </summary>
  18. public static string FtpServerIP = string.Empty;
  19. /// <summary>
  20. /// Ftp 指定用户名
  21. /// </summary>
  22. public static string FtpUserID = string.Empty;
  23. /// <summary>
  24. /// Ftp 指定用户密码
  25. /// </summary>
  26. public static string FtpPassword = string.Empty;
  27. #endregion
  28. #region 从FTP服务器下载文件,指定本地路径和本地文件名
  29. /// <summary>
  30. /// 从FTP服务器下载文件,指定本地路径和本地文件名
  31. /// </summary>
  32. /// <param name="remoteFileName">远程文件名</param>
  33. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  34. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  35. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  36. /// <returns>是否下载成功</returns>
  37. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress = null)
  38. {
  39. FtpWebRequest reqFTP, ftpsize;
  40. Stream ftpStream = null;
  41. FtpWebResponse response = null;
  42. FileStream outputStream = null;
  43. try
  44. {
  45. outputStream = new FileStream(localFileName, FileMode.Create);
  46. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  47. {
  48. throw new Exception("ftp下载目标服务器地址未设置!");
  49. }
  50. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);
  51. ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);
  52. ftpsize.UseBinary = true;
  53. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  54. reqFTP.UseBinary = true;
  55. reqFTP.KeepAlive = false;
  56. if (ifCredential)//使用用户身份认证
  57. {
  58. ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  59. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  60. }
  61. ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;
  62. FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();
  63. long totalBytes = re.ContentLength;
  64. re.Close();
  65. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  66. response = (FtpWebResponse)reqFTP.GetResponse();
  67. ftpStream = response.GetResponseStream();
  68. //更新进度
  69. if (updateProgress != null)
  70. {
  71. updateProgress((int)totalBytes, 0);//更新进度条
  72. }
  73. long totalDownloadedByte = 0;
  74. int bufferSize = 2048;
  75. int readCount;
  76. byte[] buffer = new byte[bufferSize];
  77. readCount = ftpStream.Read(buffer, 0, bufferSize);
  78. while (readCount > 0)
  79. {
  80. totalDownloadedByte = readCount + totalDownloadedByte;
  81. outputStream.Write(buffer, 0, readCount);
  82. //更新进度
  83. if (updateProgress != null)
  84. {
  85. updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条
  86. }
  87. readCount = ftpStream.Read(buffer, 0, bufferSize);
  88. }
  89. ftpStream.Close();
  90. outputStream.Close();
  91. response.Close();
  92. return true;
  93. }
  94. catch (Exception)
  95. {
  96. return false;
  97. throw;
  98. }
  99. finally
  100. {
  101. if (ftpStream != null)
  102. {
  103. ftpStream.Close();
  104. }
  105. if (outputStream != null)
  106. {
  107. outputStream.Close();
  108. }
  109. if (response != null)
  110. {
  111. response.Close();
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 从FTP服务器下载文件,指定本地路径和本地文件名(支持断点下载)
  117. /// </summary>
  118. /// <param name="remoteFileName">远程文件名</param>
  119. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  120. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  121. /// <param name="size">已下载文件流大小</param>
  122. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  123. /// <returns>是否下载成功</returns>
  124. public static bool FtpBrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress = null)
  125. {
  126. FtpWebRequest reqFTP, ftpsize;
  127. Stream ftpStream = null;
  128. FtpWebResponse response = null;
  129. FileStream outputStream = null;
  130. try
  131. {
  132. outputStream = new FileStream(localFileName, FileMode.Append);
  133. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  134. {
  135. throw new Exception("ftp下载目标服务器地址未设置!");
  136. }
  137. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);
  138. ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);
  139. ftpsize.UseBinary = true;
  140. ftpsize.ContentOffset = size;
  141. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  142. reqFTP.UseBinary = true;
  143. reqFTP.KeepAlive = false;
  144. reqFTP.ContentOffset = size;
  145. if (ifCredential)//使用用户身份认证
  146. {
  147. ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  148. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  149. }
  150. ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;
  151. FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();
  152. long totalBytes = re.ContentLength;
  153. re.Close();
  154. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  155. response = (FtpWebResponse)reqFTP.GetResponse();
  156. ftpStream = response.GetResponseStream();
  157. //更新进度
  158. if (updateProgress != null)
  159. {
  160. updateProgress((int)totalBytes, 0);//更新进度条
  161. }
  162. long totalDownloadedByte = 0;
  163. int bufferSize = 2048;
  164. int readCount;
  165. byte[] buffer = new byte[bufferSize];
  166. readCount = ftpStream.Read(buffer, 0, bufferSize);
  167. while (readCount > 0)
  168. {
  169. totalDownloadedByte = readCount + totalDownloadedByte;
  170. outputStream.Write(buffer, 0, readCount);
  171. //更新进度
  172. if (updateProgress != null)
  173. {
  174. updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条
  175. }
  176. readCount = ftpStream.Read(buffer, 0, bufferSize);
  177. }
  178. ftpStream.Close();
  179. outputStream.Close();
  180. response.Close();
  181. return true;
  182. }
  183. catch (Exception)
  184. {
  185. return false;
  186. throw;
  187. }
  188. finally
  189. {
  190. if (ftpStream != null)
  191. {
  192. ftpStream.Close();
  193. }
  194. if (outputStream != null)
  195. {
  196. outputStream.Close();
  197. }
  198. if (response != null)
  199. {
  200. response.Close();
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 从FTP服务器下载文件,指定本地路径和本地文件名
  206. /// </summary>
  207. /// <param name="remoteFileName">远程文件名</param>
  208. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  209. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  210. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  211. /// <param name="brokenOpen">是否断点下载:true 会在localFileName 找是否存在已经下载的文件,并计算文件流大小</param>
  212. /// <returns>是否下载成功</returns>
  213. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress = null)
  214. {
  215. if (brokenOpen)
  216. {
  217. try
  218. {
  219. long size = 0;
  220. if (File.Exists(localFileName))
  221. {
  222. using (FileStream outputStream = new FileStream(localFileName, FileMode.Open))
  223. {
  224. size = outputStream.Length;
  225. }
  226. }
  227. return FtpBrokenDownload(remoteFileName, localFileName, ifCredential, size, updateProgress);
  228. }
  229. catch
  230. {
  231. throw;
  232. }
  233. }
  234. else
  235. {
  236. return FtpDownload(remoteFileName, localFileName, ifCredential, updateProgress);
  237. }
  238. }
  239. #endregion
  240. #region 上传文件到FTP服务器
  241. /// <summary>
  242. /// 上传文件到FTP服务器
  243. /// </summary>
  244. /// <param name="localFullPath">本地带有完整路径的文件名</param>
  245. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  246. /// <returns>是否下载成功</returns>
  247. public static bool FtpUploadFile(string localFullPathName, Action<int, int> updateProgress = null)
  248. {
  249. FtpWebRequest reqFTP;
  250. Stream stream = null;
  251. FtpWebResponse response = null;
  252. FileStream fs = null;
  253. try
  254. {
  255. FileInfo finfo = new FileInfo(localFullPathName);
  256. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  257. {
  258. throw new Exception("ftp上传目标服务器地址未设置!");
  259. }
  260. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);
  261. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  262. reqFTP.KeepAlive = false;
  263. reqFTP.UseBinary = true;
  264. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码
  265. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服务器发出下载请求命令
  266. reqFTP.ContentLength = finfo.Length;//为request指定上传文件的大小
  267. response = reqFTP.GetResponse() as FtpWebResponse;
  268. reqFTP.ContentLength = finfo.Length;
  269. int buffLength = 1024;
  270. byte[] buff = new byte[buffLength];
  271. int contentLen;
  272. fs = finfo.OpenRead();
  273. stream = reqFTP.GetRequestStream();
  274. contentLen = fs.Read(buff, 0, buffLength);
  275. int allbye = (int)finfo.Length;
  276. //更新进度
  277. if (updateProgress != null)
  278. {
  279. updateProgress((int)allbye, 0);//更新进度条
  280. }
  281. int startbye = 0;
  282. while (contentLen != 0)
  283. {
  284. startbye = contentLen + startbye;
  285. stream.Write(buff, 0, contentLen);
  286. //更新进度
  287. if (updateProgress != null)
  288. {
  289. updateProgress((int)allbye, (int)startbye);//更新进度条
  290. }
  291. contentLen = fs.Read(buff, 0, buffLength);
  292. }
  293. stream.Close();
  294. fs.Close();
  295. response.Close();
  296. return true;
  297. }
  298. catch (Exception)
  299. {
  300. return false;
  301. throw;
  302. }
  303. finally
  304. {
  305. if (fs != null)
  306. {
  307. fs.Close();
  308. }
  309. if (stream != null)
  310. {
  311. stream.Close();
  312. }
  313. if (response != null)
  314. {
  315. response.Close();
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// 上传文件到FTP服务器(断点续传)
  321. /// </summary>
  322. /// <param name="localFullPath">本地文件全路径名称:C:\Users\JianKunKing\Desktop\IronPython脚本测试工具</param>
  323. /// <param name="remoteFilepath">远程文件所在文件夹路径</param>
  324. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  325. /// <returns></returns>
  326. public static bool FtpUploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress = null)
  327. {
  328. if (remoteFilepath == null)
  329. {
  330. remoteFilepath = "";
  331. }
  332. string newFileName = string.Empty;
  333. bool success = true;
  334. FileInfo fileInf = new FileInfo(localFullPath);
  335. long allbye = (long)fileInf.Length;
  336. if (fileInf.Name.IndexOf("#") == -1)
  337. {
  338. newFileName = RemoveSpaces(fileInf.Name);
  339. }
  340. else
  341. {
  342. newFileName = fileInf.Name.Replace("#", "#");
  343. newFileName = RemoveSpaces(newFileName);
  344. }
  345. long startfilesize = GetFileSize(newFileName, remoteFilepath);
  346. if (startfilesize >= allbye)
  347. {
  348. return false;
  349. }
  350. long startbye = startfilesize;
  351. //更新进度
  352. if (updateProgress != null)
  353. {
  354. updateProgress((int)allbye, (int)startfilesize);//更新进度条
  355. }
  356. string uri;
  357. if (remoteFilepath.Length == 0)
  358. {
  359. uri = "ftp://" + FtpServerIP + "/" + newFileName;
  360. }
  361. else
  362. {
  363. uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + newFileName;
  364. }
  365. FtpWebRequest reqFTP;
  366. // 根据uri创建FtpWebRequest对象
  367. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  368. // ftp用户名和密码
  369. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  370. // 默认为true,连接不会被关闭
  371. // 在一个命令之后被执行
  372. reqFTP.KeepAlive = false;
  373. // 指定执行什么命令
  374. reqFTP.Method = WebRequestMethods.Ftp.AppendFile;
  375. // 指定数据传输类型
  376. reqFTP.UseBinary = true;
  377. // 上传文件时通知服务器文件的大小
  378. reqFTP.ContentLength = fileInf.Length;
  379. int buffLength = 2048;// 缓冲大小设置为2kb
  380. byte[] buff = new byte[buffLength];
  381. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  382. FileStream fs = fileInf.OpenRead();
  383. Stream strm = null;
  384. try
  385. {
  386. // 把上传的文件写入流
  387. strm = reqFTP.GetRequestStream();
  388. // 每次读文件流的2kb
  389. fs.Seek(startfilesize, 0);
  390. int contentLen = fs.Read(buff, 0, buffLength);
  391. // 流内容没有结束
  392. while (contentLen != 0)
  393. {
  394. // 把内容从file stream 写入 upload stream
  395. strm.Write(buff, 0, contentLen);
  396. contentLen = fs.Read(buff, 0, buffLength);
  397. startbye += contentLen;
  398. //更新进度
  399. if (updateProgress != null)
  400. {
  401. updateProgress((int)allbye, (int)startbye);//更新进度条
  402. }
  403. }
  404. // 关闭两个流
  405. strm.Close();
  406. fs.Close();
  407. }
  408. catch
  409. {
  410. success = false;
  411. throw;
  412. }
  413. finally
  414. {
  415. if (fs != null)
  416. {
  417. fs.Close();
  418. }
  419. if (strm != null)
  420. {
  421. strm.Close();
  422. }
  423. }
  424. return success;
  425. }
  426. /// <summary>
  427. /// 去除空格
  428. /// </summary>
  429. /// <param name="str"></param>
  430. /// <returns></returns>
  431. private static string RemoveSpaces(string str)
  432. {
  433. string a = "";
  434. CharEnumerator CEnumerator = str.GetEnumerator();
  435. while (CEnumerator.MoveNext())
  436. {
  437. byte[] array = new byte[1];
  438. array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
  439. int asciicode = (short)(array[0]);
  440. if (asciicode != 32)
  441. {
  442. a += CEnumerator.Current.ToString();
  443. }
  444. }
  445. string sdate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString()
  446. + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString();
  447. return a.Split('.')[a.Split('.').Length - 2] + "." + a.Split('.')[a.Split('.').Length - 1];
  448. }
  449. /// <summary>
  450. /// 获取已上传文件大小
  451. /// </summary>
  452. /// <param name="filename">文件名称</param>
  453. /// <param name="path">服务器文件路径</param>
  454. /// <returns></returns>
  455. public static long GetFileSize(string filename, string remoteFilepath)
  456. {
  457. long filesize = 0;
  458. try
  459. {
  460. FtpWebRequest reqFTP;
  461. FileInfo fi = new FileInfo(filename);
  462. string uri;
  463. if (remoteFilepath.Length == 0)
  464. {
  465. uri = "ftp://" + FtpServerIP + "/" + fi.Name;
  466. }
  467. else
  468. {
  469. uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + fi.Name;
  470. }
  471. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  472. reqFTP.KeepAlive = false;
  473. reqFTP.UseBinary = true;
  474. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码
  475. reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
  476. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  477. filesize = response.ContentLength;
  478. return filesize;
  479. }
  480. catch
  481. {
  482. return 0;
  483. }
  484. }
  485. //public void Connect(String path, string ftpUserID, string ftpPassword)//连接ftp
  486. //{
  487. //    // 根据uri创建FtpWebRequest对象
  488. //    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
  489. //    // 指定数据传输类型
  490. //    reqFTP.UseBinary = true;
  491. //    // ftp用户名和密码
  492. //    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  493. //}
  494. #endregion
  495. }
  496. }</pre><br></pre>
  1. <pre class="csharp" name="code">using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. namespace JianKunKing.Common.Ftp
  8. {
  9. /// <summary>
  10. /// ftp方式文件下载上传
  11. /// </summary>
  12. public static class FileUpDownload
  13. {
  14. #region 变量属性
  15. /// <summary>
  16. /// Ftp服务器ip
  17. /// </summary>
  18. public static string FtpServerIP = string.Empty;
  19. /// <summary>
  20. /// Ftp 指定用户名
  21. /// </summary>
  22. public static string FtpUserID = string.Empty;
  23. /// <summary>
  24. /// Ftp 指定用户密码
  25. /// </summary>
  26. public static string FtpPassword = string.Empty;
  27. #endregion
  28. #region 从FTP服务器下载文件,指定本地路径和本地文件名
  29. /// <summary>
  30. /// 从FTP服务器下载文件,指定本地路径和本地文件名
  31. /// </summary>
  32. /// <param name="remoteFileName">远程文件名</param>
  33. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  34. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  35. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  36. /// <returns>是否下载成功</returns>
  37. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress = null)
  38. {
  39. FtpWebRequest reqFTP, ftpsize;
  40. Stream ftpStream = null;
  41. FtpWebResponse response = null;
  42. FileStream outputStream = null;
  43. try
  44. {
  45. outputStream = new FileStream(localFileName, FileMode.Create);
  46. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  47. {
  48. throw new Exception("ftp下载目标服务器地址未设置!");
  49. }
  50. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);
  51. ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);
  52. ftpsize.UseBinary = true;
  53. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  54. reqFTP.UseBinary = true;
  55. reqFTP.KeepAlive = false;
  56. if (ifCredential)//使用用户身份认证
  57. {
  58. ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  59. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  60. }
  61. ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;
  62. FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();
  63. long totalBytes = re.ContentLength;
  64. re.Close();
  65. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  66. response = (FtpWebResponse)reqFTP.GetResponse();
  67. ftpStream = response.GetResponseStream();
  68. //更新进度
  69. if (updateProgress != null)
  70. {
  71. updateProgress((int)totalBytes, 0);//更新进度条
  72. }
  73. long totalDownloadedByte = 0;
  74. int bufferSize = 2048;
  75. int readCount;
  76. byte[] buffer = new byte[bufferSize];
  77. readCount = ftpStream.Read(buffer, 0, bufferSize);
  78. while (readCount > 0)
  79. {
  80. totalDownloadedByte = readCount + totalDownloadedByte;
  81. outputStream.Write(buffer, 0, readCount);
  82. //更新进度
  83. if (updateProgress != null)
  84. {
  85. updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条
  86. }
  87. readCount = ftpStream.Read(buffer, 0, bufferSize);
  88. }
  89. ftpStream.Close();
  90. outputStream.Close();
  91. response.Close();
  92. return true;
  93. }
  94. catch (Exception)
  95. {
  96. return false;
  97. throw;
  98. }
  99. finally
  100. {
  101. if (ftpStream != null)
  102. {
  103. ftpStream.Close();
  104. }
  105. if (outputStream != null)
  106. {
  107. outputStream.Close();
  108. }
  109. if (response != null)
  110. {
  111. response.Close();
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 从FTP服务器下载文件,指定本地路径和本地文件名(支持断点下载)
  117. /// </summary>
  118. /// <param name="remoteFileName">远程文件名</param>
  119. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  120. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  121. /// <param name="size">已下载文件流大小</param>
  122. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  123. /// <returns>是否下载成功</returns>
  124. public static bool FtpBrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress = null)
  125. {
  126. FtpWebRequest reqFTP, ftpsize;
  127. Stream ftpStream = null;
  128. FtpWebResponse response = null;
  129. FileStream outputStream = null;
  130. try
  131. {
  132. outputStream = new FileStream(localFileName, FileMode.Append);
  133. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  134. {
  135. throw new Exception("ftp下载目标服务器地址未设置!");
  136. }
  137. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);
  138. ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);
  139. ftpsize.UseBinary = true;
  140. ftpsize.ContentOffset = size;
  141. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  142. reqFTP.UseBinary = true;
  143. reqFTP.KeepAlive = false;
  144. reqFTP.ContentOffset = size;
  145. if (ifCredential)//使用用户身份认证
  146. {
  147. ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  148. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  149. }
  150. ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;
  151. FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();
  152. long totalBytes = re.ContentLength;
  153. re.Close();
  154. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  155. response = (FtpWebResponse)reqFTP.GetResponse();
  156. ftpStream = response.GetResponseStream();
  157. //更新进度
  158. if (updateProgress != null)
  159. {
  160. updateProgress((int)totalBytes, 0);//更新进度条
  161. }
  162. long totalDownloadedByte = 0;
  163. int bufferSize = 2048;
  164. int readCount;
  165. byte[] buffer = new byte[bufferSize];
  166. readCount = ftpStream.Read(buffer, 0, bufferSize);
  167. while (readCount > 0)
  168. {
  169. totalDownloadedByte = readCount + totalDownloadedByte;
  170. outputStream.Write(buffer, 0, readCount);
  171. //更新进度
  172. if (updateProgress != null)
  173. {
  174. updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条
  175. }
  176. readCount = ftpStream.Read(buffer, 0, bufferSize);
  177. }
  178. ftpStream.Close();
  179. outputStream.Close();
  180. response.Close();
  181. return true;
  182. }
  183. catch (Exception)
  184. {
  185. return false;
  186. throw;
  187. }
  188. finally
  189. {
  190. if (ftpStream != null)
  191. {
  192. ftpStream.Close();
  193. }
  194. if (outputStream != null)
  195. {
  196. outputStream.Close();
  197. }
  198. if (response != null)
  199. {
  200. response.Close();
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 从FTP服务器下载文件,指定本地路径和本地文件名
  206. /// </summary>
  207. /// <param name="remoteFileName">远程文件名</param>
  208. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  209. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  210. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  211. /// <param name="brokenOpen">是否断点下载:true 会在localFileName 找是否存在已经下载的文件,并计算文件流大小</param>
  212. /// <returns>是否下载成功</returns>
  213. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress = null)
  214. {
  215. if (brokenOpen)
  216. {
  217. try
  218. {
  219. long size = 0;
  220. if (File.Exists(localFileName))
  221. {
  222. using (FileStream outputStream = new FileStream(localFileName, FileMode.Open))
  223. {
  224. size = outputStream.Length;
  225. }
  226. }
  227. return FtpBrokenDownload(remoteFileName, localFileName, ifCredential, size, updateProgress);
  228. }
  229. catch
  230. {
  231. throw;
  232. }
  233. }
  234. else
  235. {
  236. return FtpDownload(remoteFileName, localFileName, ifCredential, updateProgress);
  237. }
  238. }
  239. #endregion
  240. #region 上传文件到FTP服务器
  241. /// <summary>
  242. /// 上传文件到FTP服务器
  243. /// </summary>
  244. /// <param name="localFullPath">本地带有完整路径的文件名</param>
  245. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  246. /// <returns>是否下载成功</returns>
  247. public static bool FtpUploadFile(string localFullPathName, Action<int, int> updateProgress = null)
  248. {
  249. FtpWebRequest reqFTP;
  250. Stream stream = null;
  251. FtpWebResponse response = null;
  252. FileStream fs = null;
  253. try
  254. {
  255. FileInfo finfo = new FileInfo(localFullPathName);
  256. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  257. {
  258. throw new Exception("ftp上传目标服务器地址未设置!");
  259. }
  260. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);
  261. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  262. reqFTP.KeepAlive = false;
  263. reqFTP.UseBinary = true;
  264. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码
  265. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服务器发出下载请求命令
  266. reqFTP.ContentLength = finfo.Length;//为request指定上传文件的大小
  267. response = reqFTP.GetResponse() as FtpWebResponse;
  268. reqFTP.ContentLength = finfo.Length;
  269. int buffLength = 1024;
  270. byte[] buff = new byte[buffLength];
  271. int contentLen;
  272. fs = finfo.OpenRead();
  273. stream = reqFTP.GetRequestStream();
  274. contentLen = fs.Read(buff, 0, buffLength);
  275. int allbye = (int)finfo.Length;
  276. //更新进度
  277. if (updateProgress != null)
  278. {
  279. updateProgress((int)allbye, 0);//更新进度条
  280. }
  281. int startbye = 0;
  282. while (contentLen != 0)
  283. {
  284. startbye = contentLen + startbye;
  285. stream.Write(buff, 0, contentLen);
  286. //更新进度
  287. if (updateProgress != null)
  288. {
  289. updateProgress((int)allbye, (int)startbye);//更新进度条
  290. }
  291. contentLen = fs.Read(buff, 0, buffLength);
  292. }
  293. stream.Close();
  294. fs.Close();
  295. response.Close();
  296. return true;
  297. }
  298. catch (Exception)
  299. {
  300. return false;
  301. throw;
  302. }
  303. finally
  304. {
  305. if (fs != null)
  306. {
  307. fs.Close();
  308. }
  309. if (stream != null)
  310. {
  311. stream.Close();
  312. }
  313. if (response != null)
  314. {
  315. response.Close();
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// 上传文件到FTP服务器(断点续传)
  321. /// </summary>
  322. /// <param name="localFullPath">本地文件全路径名称:C:\Users\JianKunKing\Desktop\IronPython脚本测试工具</param>
  323. /// <param name="remoteFilepath">远程文件所在文件夹路径</param>
  324. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  325. /// <returns></returns>
  326. public static bool FtpUploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress = null)
  327. {
  328. if (remoteFilepath == null)
  329. {
  330. remoteFilepath = "";
  331. }
  332. string newFileName = string.Empty;
  333. bool success = true;
  334. FileInfo fileInf = new FileInfo(localFullPath);
  335. long allbye = (long)fileInf.Length;
  336. if (fileInf.Name.IndexOf("#") == -1)
  337. {
  338. newFileName = RemoveSpaces(fileInf.Name);
  339. }
  340. else
  341. {
  342. newFileName = fileInf.Name.Replace("#", "#");
  343. newFileName = RemoveSpaces(newFileName);
  344. }
  345. long startfilesize = GetFileSize(newFileName, remoteFilepath);
  346. if (startfilesize >= allbye)
  347. {
  348. return false;
  349. }
  350. long startbye = startfilesize;
  351. //更新进度
  352. if (updateProgress != null)
  353. {
  354. updateProgress((int)allbye, (int)startfilesize);//更新进度条
  355. }
  356. string uri;
  357. if (remoteFilepath.Length == 0)
  358. {
  359. uri = "ftp://" + FtpServerIP + "/" + newFileName;
  360. }
  361. else
  362. {
  363. uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + newFileName;
  364. }
  365. FtpWebRequest reqFTP;
  366. // 根据uri创建FtpWebRequest对象
  367. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  368. // ftp用户名和密码
  369. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  370. // 默认为true,连接不会被关闭
  371. // 在一个命令之后被执行
  372. reqFTP.KeepAlive = false;
  373. // 指定执行什么命令
  374. reqFTP.Method = WebRequestMethods.Ftp.AppendFile;
  375. // 指定数据传输类型
  376. reqFTP.UseBinary = true;
  377. // 上传文件时通知服务器文件的大小
  378. reqFTP.ContentLength = fileInf.Length;
  379. int buffLength = 2048;// 缓冲大小设置为2kb
  380. byte[] buff = new byte[buffLength];
  381. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  382. FileStream fs = fileInf.OpenRead();
  383. Stream strm = null;
  384. try
  385. {
  386. // 把上传的文件写入流
  387. strm = reqFTP.GetRequestStream();
  388. // 每次读文件流的2kb
  389. fs.Seek(startfilesize, 0);
  390. int contentLen = fs.Read(buff, 0, buffLength);
  391. // 流内容没有结束
  392. while (contentLen != 0)
  393. {
  394. // 把内容从file stream 写入 upload stream
  395. strm.Write(buff, 0, contentLen);
  396. contentLen = fs.Read(buff, 0, buffLength);
  397. startbye += contentLen;
  398. //更新进度
  399. if (updateProgress != null)
  400. {
  401. updateProgress((int)allbye, (int)startbye);//更新进度条
  402. }
  403. }
  404. // 关闭两个流
  405. strm.Close();
  406. fs.Close();
  407. }
  408. catch
  409. {
  410. success = false;
  411. throw;
  412. }
  413. finally
  414. {
  415. if (fs != null)
  416. {
  417. fs.Close();
  418. }
  419. if (strm != null)
  420. {
  421. strm.Close();
  422. }
  423. }
  424. return success;
  425. }
  426. /// <summary>
  427. /// 去除空格
  428. /// </summary>
  429. /// <param name="str"></param>
  430. /// <returns></returns>
  431. private static string RemoveSpaces(string str)
  432. {
  433. string a = "";
  434. CharEnumerator CEnumerator = str.GetEnumerator();
  435. while (CEnumerator.MoveNext())
  436. {
  437. byte[] array = new byte[1];
  438. array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
  439. int asciicode = (short)(array[0]);
  440. if (asciicode != 32)
  441. {
  442. a += CEnumerator.Current.ToString();
  443. }
  444. }
  445. string sdate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString()
  446. + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString();
  447. return a.Split('.')[a.Split('.').Length - 2] + "." + a.Split('.')[a.Split('.').Length - 1];
  448. }
  449. /// <summary>
  450. /// 获取已上传文件大小
  451. /// </summary>
  452. /// <param name="filename">文件名称</param>
  453. /// <param name="path">服务器文件路径</param>
  454. /// <returns></returns>
  455. public static long GetFileSize(string filename, string remoteFilepath)
  456. {
  457. long filesize = 0;
  458. try
  459. {
  460. FtpWebRequest reqFTP;
  461. FileInfo fi = new FileInfo(filename);
  462. string uri;
  463. if (remoteFilepath.Length == 0)
  464. {
  465. uri = "ftp://" + FtpServerIP + "/" + fi.Name;
  466. }
  467. else
  468. {
  469. uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + fi.Name;
  470. }
  471. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  472. reqFTP.KeepAlive = false;
  473. reqFTP.UseBinary = true;
  474. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码
  475. reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
  476. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  477. filesize = response.ContentLength;
  478. return filesize;
  479. }
  480. catch
  481. {
  482. return 0;
  483. }
  484. }
  485. //public void Connect(String path, string ftpUserID, string ftpPassword)//连接ftp
  486. //{
  487. //    // 根据uri创建FtpWebRequest对象
  488. //    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
  489. //    // 指定数据传输类型
  490. //    reqFTP.UseBinary = true;
  491. //    // ftp用户名和密码
  492. //    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  493. //}
  494. #endregion
  495. }
  496. }</pre><br>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. namespace JianKunKing.Common.Ftp
  8. {
  9. /// <summary>
  10. /// ftp方式文件下载上传
  11. /// </summary>
  12. public static class FileUpDownload
  13. {
  14. #region 变量属性
  15. /// <summary>
  16. /// Ftp服务器ip
  17. /// </summary>
  18. public static string FtpServerIP = string.Empty;
  19. /// <summary>
  20. /// Ftp 指定用户名
  21. /// </summary>
  22. public static string FtpUserID = string.Empty;
  23. /// <summary>
  24. /// Ftp 指定用户密码
  25. /// </summary>
  26. public static string FtpPassword = string.Empty;
  27. #endregion
  28. #region 从FTP服务器下载文件,指定本地路径和本地文件名
  29. /// <summary>
  30. /// 从FTP服务器下载文件,指定本地路径和本地文件名
  31. /// </summary>
  32. /// <param name="remoteFileName">远程文件名</param>
  33. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  34. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  35. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  36. /// <returns>是否下载成功</returns>
  37. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress = null)
  38. {
  39. FtpWebRequest reqFTP, ftpsize;
  40. Stream ftpStream = null;
  41. FtpWebResponse response = null;
  42. FileStream outputStream = null;
  43. try
  44. {
  45. outputStream = new FileStream(localFileName, FileMode.Create);
  46. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  47. {
  48. throw new Exception("ftp下载目标服务器地址未设置!");
  49. }
  50. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);
  51. ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);
  52. ftpsize.UseBinary = true;
  53. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  54. reqFTP.UseBinary = true;
  55. reqFTP.KeepAlive = false;
  56. if (ifCredential)//使用用户身份认证
  57. {
  58. ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  59. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  60. }
  61. ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;
  62. FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();
  63. long totalBytes = re.ContentLength;
  64. re.Close();
  65. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  66. response = (FtpWebResponse)reqFTP.GetResponse();
  67. ftpStream = response.GetResponseStream();
  68. //更新进度
  69. if (updateProgress != null)
  70. {
  71. updateProgress((int)totalBytes, 0);//更新进度条
  72. }
  73. long totalDownloadedByte = 0;
  74. int bufferSize = 2048;
  75. int readCount;
  76. byte[] buffer = new byte[bufferSize];
  77. readCount = ftpStream.Read(buffer, 0, bufferSize);
  78. while (readCount > 0)
  79. {
  80. totalDownloadedByte = readCount + totalDownloadedByte;
  81. outputStream.Write(buffer, 0, readCount);
  82. //更新进度
  83. if (updateProgress != null)
  84. {
  85. updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条
  86. }
  87. readCount = ftpStream.Read(buffer, 0, bufferSize);
  88. }
  89. ftpStream.Close();
  90. outputStream.Close();
  91. response.Close();
  92. return true;
  93. }
  94. catch (Exception)
  95. {
  96. return false;
  97. throw;
  98. }
  99. finally
  100. {
  101. if (ftpStream != null)
  102. {
  103. ftpStream.Close();
  104. }
  105. if (outputStream != null)
  106. {
  107. outputStream.Close();
  108. }
  109. if (response != null)
  110. {
  111. response.Close();
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 从FTP服务器下载文件,指定本地路径和本地文件名(支持断点下载)
  117. /// </summary>
  118. /// <param name="remoteFileName">远程文件名</param>
  119. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  120. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  121. /// <param name="size">已下载文件流大小</param>
  122. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  123. /// <returns>是否下载成功</returns>
  124. public static bool FtpBrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress = null)
  125. {
  126. FtpWebRequest reqFTP, ftpsize;
  127. Stream ftpStream = null;
  128. FtpWebResponse response = null;
  129. FileStream outputStream = null;
  130. try
  131. {
  132. outputStream = new FileStream(localFileName, FileMode.Append);
  133. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  134. {
  135. throw new Exception("ftp下载目标服务器地址未设置!");
  136. }
  137. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);
  138. ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);
  139. ftpsize.UseBinary = true;
  140. ftpsize.ContentOffset = size;
  141. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  142. reqFTP.UseBinary = true;
  143. reqFTP.KeepAlive = false;
  144. reqFTP.ContentOffset = size;
  145. if (ifCredential)//使用用户身份认证
  146. {
  147. ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  148. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  149. }
  150. ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;
  151. FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();
  152. long totalBytes = re.ContentLength;
  153. re.Close();
  154. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  155. response = (FtpWebResponse)reqFTP.GetResponse();
  156. ftpStream = response.GetResponseStream();
  157. //更新进度
  158. if (updateProgress != null)
  159. {
  160. updateProgress((int)totalBytes, 0);//更新进度条
  161. }
  162. long totalDownloadedByte = 0;
  163. int bufferSize = 2048;
  164. int readCount;
  165. byte[] buffer = new byte[bufferSize];
  166. readCount = ftpStream.Read(buffer, 0, bufferSize);
  167. while (readCount > 0)
  168. {
  169. totalDownloadedByte = readCount + totalDownloadedByte;
  170. outputStream.Write(buffer, 0, readCount);
  171. //更新进度
  172. if (updateProgress != null)
  173. {
  174. updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条
  175. }
  176. readCount = ftpStream.Read(buffer, 0, bufferSize);
  177. }
  178. ftpStream.Close();
  179. outputStream.Close();
  180. response.Close();
  181. return true;
  182. }
  183. catch (Exception)
  184. {
  185. return false;
  186. throw;
  187. }
  188. finally
  189. {
  190. if (ftpStream != null)
  191. {
  192. ftpStream.Close();
  193. }
  194. if (outputStream != null)
  195. {
  196. outputStream.Close();
  197. }
  198. if (response != null)
  199. {
  200. response.Close();
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 从FTP服务器下载文件,指定本地路径和本地文件名
  206. /// </summary>
  207. /// <param name="remoteFileName">远程文件名</param>
  208. /// <param name="localFileName">保存本地的文件名(包含路径)</param>
  209. /// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
  210. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  211. /// <param name="brokenOpen">是否断点下载:true 会在localFileName 找是否存在已经下载的文件,并计算文件流大小</param>
  212. /// <returns>是否下载成功</returns>
  213. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress = null)
  214. {
  215. if (brokenOpen)
  216. {
  217. try
  218. {
  219. long size = 0;
  220. if (File.Exists(localFileName))
  221. {
  222. using (FileStream outputStream = new FileStream(localFileName, FileMode.Open))
  223. {
  224. size = outputStream.Length;
  225. }
  226. }
  227. return FtpBrokenDownload(remoteFileName, localFileName, ifCredential, size, updateProgress);
  228. }
  229. catch
  230. {
  231. throw;
  232. }
  233. }
  234. else
  235. {
  236. return FtpDownload(remoteFileName, localFileName, ifCredential, updateProgress);
  237. }
  238. }
  239. #endregion
  240. #region 上传文件到FTP服务器
  241. /// <summary>
  242. /// 上传文件到FTP服务器
  243. /// </summary>
  244. /// <param name="localFullPath">本地带有完整路径的文件名</param>
  245. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  246. /// <returns>是否下载成功</returns>
  247. public static bool FtpUploadFile(string localFullPathName, Action<int, int> updateProgress = null)
  248. {
  249. FtpWebRequest reqFTP;
  250. Stream stream = null;
  251. FtpWebResponse response = null;
  252. FileStream fs = null;
  253. try
  254. {
  255. FileInfo finfo = new FileInfo(localFullPathName);
  256. if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
  257. {
  258. throw new Exception("ftp上传目标服务器地址未设置!");
  259. }
  260. Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);
  261. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  262. reqFTP.KeepAlive = false;
  263. reqFTP.UseBinary = true;
  264. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码
  265. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服务器发出下载请求命令
  266. reqFTP.ContentLength = finfo.Length;//为request指定上传文件的大小
  267. response = reqFTP.GetResponse() as FtpWebResponse;
  268. reqFTP.ContentLength = finfo.Length;
  269. int buffLength = 1024;
  270. byte[] buff = new byte[buffLength];
  271. int contentLen;
  272. fs = finfo.OpenRead();
  273. stream = reqFTP.GetRequestStream();
  274. contentLen = fs.Read(buff, 0, buffLength);
  275. int allbye = (int)finfo.Length;
  276. //更新进度
  277. if (updateProgress != null)
  278. {
  279. updateProgress((int)allbye, 0);//更新进度条
  280. }
  281. int startbye = 0;
  282. while (contentLen != 0)
  283. {
  284. startbye = contentLen + startbye;
  285. stream.Write(buff, 0, contentLen);
  286. //更新进度
  287. if (updateProgress != null)
  288. {
  289. updateProgress((int)allbye, (int)startbye);//更新进度条
  290. }
  291. contentLen = fs.Read(buff, 0, buffLength);
  292. }
  293. stream.Close();
  294. fs.Close();
  295. response.Close();
  296. return true;
  297. }
  298. catch (Exception)
  299. {
  300. return false;
  301. throw;
  302. }
  303. finally
  304. {
  305. if (fs != null)
  306. {
  307. fs.Close();
  308. }
  309. if (stream != null)
  310. {
  311. stream.Close();
  312. }
  313. if (response != null)
  314. {
  315. response.Close();
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// 上传文件到FTP服务器(断点续传)
  321. /// </summary>
  322. /// <param name="localFullPath">本地文件全路径名称:C:\Users\JianKunKing\Desktop\IronPython脚本测试工具</param>
  323. /// <param name="remoteFilepath">远程文件所在文件夹路径</param>
  324. /// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
  325. /// <returns></returns>
  326. public static bool FtpUploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress = null)
  327. {
  328. if (remoteFilepath == null)
  329. {
  330. remoteFilepath = "";
  331. }
  332. string newFileName = string.Empty;
  333. bool success = true;
  334. FileInfo fileInf = new FileInfo(localFullPath);
  335. long allbye = (long)fileInf.Length;
  336. if (fileInf.Name.IndexOf("#") == -1)
  337. {
  338. newFileName = RemoveSpaces(fileInf.Name);
  339. }
  340. else
  341. {
  342. newFileName = fileInf.Name.Replace("#", "#");
  343. newFileName = RemoveSpaces(newFileName);
  344. }
  345. long startfilesize = GetFileSize(newFileName, remoteFilepath);
  346. if (startfilesize >= allbye)
  347. {
  348. return false;
  349. }
  350. long startbye = startfilesize;
  351. //更新进度
  352. if (updateProgress != null)
  353. {
  354. updateProgress((int)allbye, (int)startfilesize);//更新进度条
  355. }
  356. string uri;
  357. if (remoteFilepath.Length == 0)
  358. {
  359. uri = "ftp://" + FtpServerIP + "/" + newFileName;
  360. }
  361. else
  362. {
  363. uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + newFileName;
  364. }
  365. FtpWebRequest reqFTP;
  366. // 根据uri创建FtpWebRequest对象
  367. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  368. // ftp用户名和密码
  369. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  370. // 默认为true,连接不会被关闭
  371. // 在一个命令之后被执行
  372. reqFTP.KeepAlive = false;
  373. // 指定执行什么命令
  374. reqFTP.Method = WebRequestMethods.Ftp.AppendFile;
  375. // 指定数据传输类型
  376. reqFTP.UseBinary = true;
  377. // 上传文件时通知服务器文件的大小
  378. reqFTP.ContentLength = fileInf.Length;
  379. int buffLength = 2048;// 缓冲大小设置为2kb
  380. byte[] buff = new byte[buffLength];
  381. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  382. FileStream fs = fileInf.OpenRead();
  383. Stream strm = null;
  384. try
  385. {
  386. // 把上传的文件写入流
  387. strm = reqFTP.GetRequestStream();
  388. // 每次读文件流的2kb
  389. fs.Seek(startfilesize, 0);
  390. int contentLen = fs.Read(buff, 0, buffLength);
  391. // 流内容没有结束
  392. while (contentLen != 0)
  393. {
  394. // 把内容从file stream 写入 upload stream
  395. strm.Write(buff, 0, contentLen);
  396. contentLen = fs.Read(buff, 0, buffLength);
  397. startbye += contentLen;
  398. //更新进度
  399. if (updateProgress != null)
  400. {
  401. updateProgress((int)allbye, (int)startbye);//更新进度条
  402. }
  403. }
  404. // 关闭两个流
  405. strm.Close();
  406. fs.Close();
  407. }
  408. catch
  409. {
  410. success = false;
  411. throw;
  412. }
  413. finally
  414. {
  415. if (fs != null)
  416. {
  417. fs.Close();
  418. }
  419. if (strm != null)
  420. {
  421. strm.Close();
  422. }
  423. }
  424. return success;
  425. }
  426. /// <summary>
  427. /// 去除空格
  428. /// </summary>
  429. /// <param name="str"></param>
  430. /// <returns></returns>
  431. private static string RemoveSpaces(string str)
  432. {
  433. string a = "";
  434. CharEnumerator CEnumerator = str.GetEnumerator();
  435. while (CEnumerator.MoveNext())
  436. {
  437. byte[] array = new byte[1];
  438. array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
  439. int asciicode = (short)(array[0]);
  440. if (asciicode != 32)
  441. {
  442. a += CEnumerator.Current.ToString();
  443. }
  444. }
  445. string sdate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString()
  446. + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString();
  447. return a.Split('.')[a.Split('.').Length - 2] + "." + a.Split('.')[a.Split('.').Length - 1];
  448. }
  449. /// <summary>
  450. /// 获取已上传文件大小
  451. /// </summary>
  452. /// <param name="filename">文件名称</param>
  453. /// <param name="path">服务器文件路径</param>
  454. /// <returns></returns>
  455. public static long GetFileSize(string filename, string remoteFilepath)
  456. {
  457. long filesize = 0;
  458. try
  459. {
  460. FtpWebRequest reqFTP;
  461. FileInfo fi = new FileInfo(filename);
  462. string uri;
  463. if (remoteFilepath.Length == 0)
  464. {
  465. uri = "ftp://" + FtpServerIP + "/" + fi.Name;
  466. }
  467. else
  468. {
  469. uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + fi.Name;
  470. }
  471. reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
  472. reqFTP.KeepAlive = false;
  473. reqFTP.UseBinary = true;
  474. reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码
  475. reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
  476. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  477. filesize = response.ContentLength;
  478. return filesize;
  479. }
  480. catch
  481. {
  482. return 0;
  483. }
  484. }
  485. //public void Connect(String path, string ftpUserID, string ftpPassword)//连接ftp
  486. //{
  487. //    // 根据uri创建FtpWebRequest对象
  488. //    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
  489. //    // 指定数据传输类型
  490. //    reqFTP.UseBinary = true;
  491. //    // ftp用户名和密码
  492. //    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  493. //}
  494. #endregion
  495. }
  496. }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO; namespace JianKunKing.Common.Ftp
{
/// <summary>
/// ftp方式文件下载上传
/// </summary>
public static class FileUpDownload
{
#region 变量属性
/// <summary>
/// Ftp服务器ip
/// </summary>
public static string FtpServerIP = string.Empty;
/// <summary>
/// Ftp 指定用户名
/// </summary>
public static string FtpUserID = string.Empty;
/// <summary>
/// Ftp 指定用户密码
/// </summary>
public static string FtpPassword = string.Empty; #endregion #region 从FTP服务器下载文件,指定本地路径和本地文件名
/// <summary>
/// 从FTP服务器下载文件,指定本地路径和本地文件名
/// </summary>
/// <param name="remoteFileName">远程文件名</param>
/// <param name="localFileName">保存本地的文件名(包含路径)</param>
/// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
/// <returns>是否下载成功</returns>
public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress = null)
{
FtpWebRequest reqFTP, ftpsize;
Stream ftpStream = null;
FtpWebResponse response = null;
FileStream outputStream = null;
try
{ outputStream = new FileStream(localFileName, FileMode.Create);
if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
{
throw new Exception("ftp下载目标服务器地址未设置!");
}
Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);
ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);
ftpsize.UseBinary = true; reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
if (ifCredential)//使用用户身份认证
{
ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
}
ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();
long totalBytes = re.ContentLength;
re.Close(); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
response = (FtpWebResponse)reqFTP.GetResponse();
ftpStream = response.GetResponseStream(); //更新进度
if (updateProgress != null)
{
updateProgress((int)totalBytes, 0);//更新进度条
}
long totalDownloadedByte = 0;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
totalDownloadedByte = readCount + totalDownloadedByte;
outputStream.Write(buffer, 0, readCount);
//更新进度
if (updateProgress != null)
{
updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条
}
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return true;
}
catch (Exception)
{
return false;
throw;
}
finally
{
if (ftpStream != null)
{
ftpStream.Close();
}
if (outputStream != null)
{
outputStream.Close();
}
if (response != null)
{
response.Close();
}
}
}
/// <summary>
/// 从FTP服务器下载文件,指定本地路径和本地文件名(支持断点下载)
/// </summary>
/// <param name="remoteFileName">远程文件名</param>
/// <param name="localFileName">保存本地的文件名(包含路径)</param>
/// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
/// <param name="size">已下载文件流大小</param>
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
/// <returns>是否下载成功</returns>
public static bool FtpBrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress = null)
{
FtpWebRequest reqFTP, ftpsize;
Stream ftpStream = null;
FtpWebResponse response = null;
FileStream outputStream = null;
try
{ outputStream = new FileStream(localFileName, FileMode.Append);
if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
{
throw new Exception("ftp下载目标服务器地址未设置!");
}
Uri uri = new Uri("ftp://" + FtpServerIP + "/" + remoteFileName);
ftpsize = (FtpWebRequest)FtpWebRequest.Create(uri);
ftpsize.UseBinary = true;
ftpsize.ContentOffset = size; reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.ContentOffset = size;
if (ifCredential)//使用用户身份认证
{
ftpsize.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
}
ftpsize.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse re = (FtpWebResponse)ftpsize.GetResponse();
long totalBytes = re.ContentLength;
re.Close(); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
response = (FtpWebResponse)reqFTP.GetResponse();
ftpStream = response.GetResponseStream(); //更新进度
if (updateProgress != null)
{
updateProgress((int)totalBytes, 0);//更新进度条
}
long totalDownloadedByte = 0;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
totalDownloadedByte = readCount + totalDownloadedByte;
outputStream.Write(buffer, 0, readCount);
//更新进度
if (updateProgress != null)
{
updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新进度条
}
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return true;
}
catch (Exception)
{
return false;
throw;
}
finally
{
if (ftpStream != null)
{
ftpStream.Close();
}
if (outputStream != null)
{
outputStream.Close();
}
if (response != null)
{
response.Close();
}
}
} /// <summary>
/// 从FTP服务器下载文件,指定本地路径和本地文件名
/// </summary>
/// <param name="remoteFileName">远程文件名</param>
/// <param name="localFileName">保存本地的文件名(包含路径)</param>
/// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
/// <param name="brokenOpen">是否断点下载:true 会在localFileName 找是否存在已经下载的文件,并计算文件流大小</param>
/// <returns>是否下载成功</returns>
public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress = null)
{
if (brokenOpen)
{
try
{
long size = 0;
if (File.Exists(localFileName))
{
using (FileStream outputStream = new FileStream(localFileName, FileMode.Open))
{
size = outputStream.Length;
}
}
return FtpBrokenDownload(remoteFileName, localFileName, ifCredential, size, updateProgress);
}
catch
{
throw;
}
}
else
{
return FtpDownload(remoteFileName, localFileName, ifCredential, updateProgress);
}
}
#endregion #region 上传文件到FTP服务器
/// <summary>
/// 上传文件到FTP服务器
/// </summary>
/// <param name="localFullPath">本地带有完整路径的文件名</param>
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
/// <returns>是否下载成功</returns>
public static bool FtpUploadFile(string localFullPathName, Action<int, int> updateProgress = null)
{
FtpWebRequest reqFTP;
Stream stream = null;
FtpWebResponse response = null;
FileStream fs = null;
try
{
FileInfo finfo = new FileInfo(localFullPathName);
if (FtpServerIP == null || FtpServerIP.Trim().Length == 0)
{
throw new Exception("ftp上传目标服务器地址未设置!");
}
Uri uri = new Uri("ftp://" + FtpServerIP + "/" + finfo.Name);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;//向服务器发出下载请求命令
reqFTP.ContentLength = finfo.Length;//为request指定上传文件的大小
response = reqFTP.GetResponse() as FtpWebResponse;
reqFTP.ContentLength = finfo.Length;
int buffLength = 1024;
byte[] buff = new byte[buffLength];
int contentLen;
fs = finfo.OpenRead();
stream = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)finfo.Length;
//更新进度
if (updateProgress != null)
{
updateProgress((int)allbye, 0);//更新进度条
}
int startbye = 0;
while (contentLen != 0)
{
startbye = contentLen + startbye;
stream.Write(buff, 0, contentLen);
//更新进度
if (updateProgress != null)
{
updateProgress((int)allbye, (int)startbye);//更新进度条
}
contentLen = fs.Read(buff, 0, buffLength);
}
stream.Close();
fs.Close();
response.Close();
return true; }
catch (Exception)
{
return false;
throw;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (stream != null)
{
stream.Close();
}
if (response != null)
{
response.Close();
}
}
} /// <summary>
/// 上传文件到FTP服务器(断点续传)
/// </summary>
/// <param name="localFullPath">本地文件全路径名称:C:\Users\JianKunKing\Desktop\IronPython脚本测试工具</param>
/// <param name="remoteFilepath">远程文件所在文件夹路径</param>
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
/// <returns></returns>
public static bool FtpUploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress = null)
{
if (remoteFilepath == null)
{
remoteFilepath = "";
}
string newFileName = string.Empty;
bool success = true;
FileInfo fileInf = new FileInfo(localFullPath);
long allbye = (long)fileInf.Length;
if (fileInf.Name.IndexOf("#") == -1)
{
newFileName = RemoveSpaces(fileInf.Name);
}
else
{
newFileName = fileInf.Name.Replace("#", "#");
newFileName = RemoveSpaces(newFileName);
}
long startfilesize = GetFileSize(newFileName, remoteFilepath);
if (startfilesize >= allbye)
{
return false;
}
long startbye = startfilesize;
//更新进度
if (updateProgress != null)
{
updateProgress((int)allbye, (int)startfilesize);//更新进度条
} string uri;
if (remoteFilepath.Length == 0)
{
uri = "ftp://" + FtpServerIP + "/" + newFileName;
}
else
{
uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + newFileName;
}
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.AppendFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;// 缓冲大小设置为2kb
byte[] buff = new byte[buffLength];
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead();
Stream strm = null;
try
{
// 把上传的文件写入流
strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
fs.Seek(startfilesize, 0);
int contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
//更新进度
if (updateProgress != null)
{
updateProgress((int)allbye, (int)startbye);//更新进度条
}
}
// 关闭两个流
strm.Close();
fs.Close();
}
catch
{
success = false;
throw;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (strm != null)
{
strm.Close();
}
}
return success;
} /// <summary>
/// 去除空格
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string RemoveSpaces(string str)
{
string a = "";
CharEnumerator CEnumerator = str.GetEnumerator();
while (CEnumerator.MoveNext())
{
byte[] array = new byte[1];
array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
int asciicode = (short)(array[0]);
if (asciicode != 32)
{
a += CEnumerator.Current.ToString();
}
}
string sdate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString()
+ System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString();
return a.Split('.')[a.Split('.').Length - 2] + "." + a.Split('.')[a.Split('.').Length - 1];
}
/// <summary>
/// 获取已上传文件大小
/// </summary>
/// <param name="filename">文件名称</param>
/// <param name="path">服务器文件路径</param>
/// <returns></returns>
public static long GetFileSize(string filename, string remoteFilepath)
{
long filesize = 0;
try
{
FtpWebRequest reqFTP;
FileInfo fi = new FileInfo(filename);
string uri;
if (remoteFilepath.Length == 0)
{
uri = "ftp://" + FtpServerIP + "/" + fi.Name;
}
else
{
uri = "ftp://" + FtpServerIP + "/" + remoteFilepath + "/" + fi.Name;
}
reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.KeepAlive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FtpUserID, FtpPassword);//用户,密码
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
filesize = response.ContentLength;
return filesize;
}
catch
{
return 0;
}
} //public void Connect(String path, string ftpUserID, string ftpPassword)//连接ftp
//{
// // 根据uri创建FtpWebRequest对象
// reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// // 指定数据传输类型
// reqFTP.UseBinary = true;
// // ftp用户名和密码
// reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
//} #endregion }
}

调用实例:

下载(不需要带iis部分的路径):

  1. FileUpDownload.FtpServerIP = "192.168.1.1";
  2. FileUpDownload.FtpUserID = "ftpTest001";
  3. FileUpDownload.FtpPassword = "aaaaaa";
  4. FileUpDownload.FtpDownload("Beyond Compare(绿色免安装).zip",
  5. Application.StartupPath + "/downloads/crm2.ra6", false);
 FileUpDownload.FtpServerIP = "192.168.1.1";
FileUpDownload.FtpUserID = "ftpTest001";
FileUpDownload.FtpPassword = "aaaaaa";
FileUpDownload.FtpDownload("Beyond Compare(绿色免安装).zip",
Application.StartupPath + "/downloads/crm2.ra6", false);

之前的上传的文件目录:

上传(不需要带iis部分的路径):

  1. OpenFileDialog op = new OpenFileDialog();
  2. op.InitialDirectory = Application.StartupPath;
  3. op.RestoreDirectory = true;
  4. op.Filter = "压缩文件(*.zip)|*.zip|压缩文件(*.rar)|*.rar|所有文件(*.*)|*.*";
  5. if (op.ShowDialog() == DialogResult.OK)
  6. {
  7. string aa = op.FileName;
  8. FileUpDownload.FtpServerIP = "192.168.1.1";
  9. FileUpDownload.FtpUserID = "ftpTest001";
  10. FileUpDownload.FtpPassword = "aaaaaa";
  11. //全路径
  12. FileUpDownload.FtpUploadFile(aa);
  13. }
OpenFileDialog op = new OpenFileDialog();
op.InitialDirectory = Application.StartupPath;
op.RestoreDirectory = true;
op.Filter = "压缩文件(*.zip)|*.zip|压缩文件(*.rar)|*.rar|所有文件(*.*)|*.*";
if (op.ShowDialog() == DialogResult.OK)
{
string aa = op.FileName;
FileUpDownload.FtpServerIP = "192.168.1.1";
FileUpDownload.FtpUserID = "ftpTest001";
FileUpDownload.FtpPassword = "aaaaaa";
//全路径
FileUpDownload.FtpUploadFile(aa);
}

IIS下搭建FTP服务器点击打开链接

参考文章:点击打开链接
小注:
多线程下载:
将文件分段,分段后,每段启用一个线程来下载(提供一个属性或者入参,来控制启用多少个线程),下载完成后,将文件拼起来(跟断点续传的原理差不多)
具体可以百度搜索:c#  ftp  多线程 下载

最新文章

  1. JavaScript继承的模拟实现
  2. wireshark 导出所有filter出来的包
  3. JS for循环
  4. STM32学习笔记(四) RCC外设的学习和理解
  5. windows环境下局域网内无法访问apache站点
  6. Java的数据转换
  7. winform 自定义控件以及委托事件的使用
  8. 表格实现hao123
  9. apache2.4 +django1.9+python3+ubuntu15.10
  10. Linux下Join命令
  11. 【codeforce 219D】 Choosing Capital for Treeland (树形DP)
  12. 激活前一个程序(注册全局消息,使用Mutex探测,如果已经占用就广播消息通知第一个程序,然后第一个程序做出响应)
  13. Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]
  14. xp安装maven
  15. linux的脚本应用for循环答应变量
  16. Java 面试知识点解析(四)——版本特性篇
  17. 在Git中添加一个项目
  18. 解决键盘输入被JDB占用的问题
  19. python学习笔记之四-多进程&amp;多线程&amp;异步非阻塞
  20. QWebView崩溃的问题

热门文章

  1. Java 核心编程技术干货,2019 最新整理版!
  2. Spring源码分析(一):从哪里开始看spring源码(系列文章基于Spring5.0)
  3. Python匹马行天下之初识python!
  4. Android笔记之调用系统相机拍照
  5. 2018-9-29-Roslyn-通过-Nuget-引用源代码-在-VS-智能提示正常但是无法编译
  6. ionic 滚动条 ion-scroll 用于创建一个可滚动的容器
  7. dos中文乱码怎么办?
  8. Linux课程---16、apache虚拟主机设置(如何在一台服务器上配置三个域名)
  9. Windows与Linux获取进程集合的方法
  10. PagedListCore的使用