当前位置:   article > 正文

C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)_c# ftp替换现有文件

c# ftp替换现有文件
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Data;
  6. using System.IO;
  7. using System.ComponentModel;
  8. namespace Common
  9. {
  10. public class FTPClient
  11. {
  12. private string ftpServerIP = String.Empty;
  13. private string ftpUser = String.Empty;
  14. private string ftpPassword = String.Empty;
  15. private string ftpRootURL = String.Empty;
  16. public FTPClient(string url, string userid,string password)
  17. {
  18. this.ftpServerIP = ftp的IP地址;
  19. this.ftpUser = 用户名;
  20. this.ftpPassword = 密码;
  21. this.ftpRootURL = "ftp://" + url + "/";
  22. }
  23. /// <summary>
  24. /// 上传
  25. /// </summary>
  26. /// <param name="localFile">本地文件绝对路径</param>
  27. /// <param name="ftpPath">上传到ftp的路径</param>
  28. /// <param name="ftpFileName">上传到ftp的文件名</param>
  29. public bool fileUpload(FileInfo localFile, string ftpPath, string ftpFileName)
  30. {
  31. bool success = false;
  32. FtpWebRequest ftpWebRequest = null;
  33. FileStream localFileStream = null;
  34. Stream requestStream = null;
  35. try
  36. {
  37. string uri = ftpRootURL + ftpPath + ftpFileName;
  38. ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  39. ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  40. ftpWebRequest.UseBinary = true;
  41. ftpWebRequest.KeepAlive = false;
  42. ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
  43. ftpWebRequest.ContentLength = localFile.Length;
  44. int buffLength = 2048;
  45. byte[] buff = new byte[buffLength];
  46. int contentLen;
  47. localFileStream = localFile.OpenRead();
  48. requestStream = ftpWebRequest.GetRequestStream();
  49. contentLen = localFileStream.Read(buff, 0, buffLength);
  50. while (contentLen != 0)
  51. {
  52. requestStream.Write(buff, 0, contentLen);
  53. contentLen = localFileStream.Read(buff, 0, buffLength);
  54. }
  55. success = true;
  56. }
  57. catch (Exception)
  58. {
  59. success = false;
  60. }
  61. finally
  62. {
  63. if (requestStream != null)
  64. {
  65. requestStream.Close();
  66. }
  67. if (localFileStream != null)
  68. {
  69. localFileStream.Close();
  70. }
  71. }
  72. return success;
  73. }
  74. /// <summary>
  75. /// 上传文件
  76. /// </summary>
  77. /// <param name="localPath">本地文件地址(没有文件名)</param>
  78. /// <param name="localFileName">本地文件名</param>
  79. /// <param name="ftpPath">上传到ftp的路径</param>
  80. /// <param name="ftpFileName">上传到ftp的文件名</param>
  81. public bool fileUpload(string localPath, string localFileName, string ftpPath, string ftpFileName)
  82. {
  83. bool success = false;
  84. try
  85. {
  86. FileInfo localFile = new FileInfo(localPath + localFileName);
  87. if (localFile.Exists)
  88. {
  89. success = fileUpload(localFile, ftpPath, ftpFileName);
  90. }
  91. else
  92. {
  93. success = false;
  94. }
  95. }
  96. catch (Exception)
  97. {
  98. success = false;
  99. }
  100. return success;
  101. }
  102. /// <summary>
  103. /// 下载文件
  104. /// </summary>
  105. /// <param name="localPath">本地文件地址(没有文件名)</param>
  106. /// <param name="localFileName">本地文件名</param>
  107. /// <param name="ftpPath">下载的ftp的路径</param>
  108. /// <param name="ftpFileName">下载的ftp的文件名</param>
  109. public bool fileDownload(string localPath, string localFileName, string ftpPath, string ftpFileName)
  110. {
  111. bool success = false;
  112. FtpWebRequest ftpWebRequest = null;
  113. FtpWebResponse ftpWebResponse = null;
  114. Stream ftpResponseStream = null;
  115. FileStream outputStream = null;
  116. try
  117. {
  118. outputStream = new FileStream(localPath + localFileName, FileMode.Create);
  119. string uri = ftpRootURL + ftpPath + ftpFileName;
  120. ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  121. ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  122. ftpWebRequest.UseBinary = true;
  123. ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  124. ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
  125. ftpResponseStream = ftpWebResponse.GetResponseStream();
  126. long contentLength = ftpWebResponse.ContentLength;
  127. int bufferSize = 2048;
  128. byte[] buffer = new byte[bufferSize];
  129. int readCount;
  130. readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
  131. while (readCount > 0)
  132. {
  133. outputStream.Write(buffer, 0, readCount);
  134. readCount = ftpResponseStream.Read(buffer, 0, bufferSize);
  135. }
  136. success = true;
  137. }
  138. catch (Exception)
  139. {
  140. success = false;
  141. }
  142. finally
  143. {
  144. if (outputStream != null)
  145. {
  146. outputStream.Close();
  147. }
  148. if (ftpResponseStream != null)
  149. {
  150. ftpResponseStream.Close();
  151. }
  152. if (ftpWebResponse != null)
  153. {
  154. ftpWebResponse.Close();
  155. }
  156. }
  157. return success;
  158. }
  159. /// <summary>
  160. /// 重命名
  161. /// </summary>
  162. /// <param name="ftpPath">ftp文件路径</param>
  163. /// <param name="currentFilename"></param>
  164. /// <param name="newFilename"></param>
  165. public bool fileRename(string ftpPath, string currentFileName, string newFileName)
  166. {
  167. bool success = false;
  168. FtpWebRequest ftpWebRequest = null;
  169. FtpWebResponse ftpWebResponse = null;
  170. Stream ftpResponseStream = null;
  171. try
  172. {
  173. string uri = ftpRootURL + ftpPath + currentFileName;
  174. ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  175. ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  176. ftpWebRequest.UseBinary = true;
  177. ftpWebRequest.Method = WebRequestMethods.Ftp.Rename;
  178. ftpWebRequest.RenameTo = newFileName;
  179. ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
  180. ftpResponseStream = ftpWebResponse.GetResponseStream();
  181. }
  182. catch (Exception)
  183. {
  184. success = false;
  185. }
  186. finally
  187. {
  188. if (ftpResponseStream != null)
  189. {
  190. ftpResponseStream.Close();
  191. }
  192. if (ftpWebResponse != null)
  193. {
  194. ftpWebResponse.Close();
  195. }
  196. }
  197. return success;
  198. }
  199. /// <summary>
  200. /// 消除文件
  201. /// </summary>
  202. /// <param name="filePath"></param>
  203. public bool fileDelete(string ftpPath, string ftpName)
  204. {
  205. bool success = false;
  206. FtpWebRequest ftpWebRequest = null;
  207. FtpWebResponse ftpWebResponse = null;
  208. Stream ftpResponseStream = null;
  209. StreamReader streamReader = null;
  210. try
  211. {
  212. string uri = ftpRootURL + ftpPath + ftpName;
  213. ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  214. ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  215. ftpWebRequest.KeepAlive = false;
  216. ftpWebRequest.Method = WebRequestMethods.Ftp.DeleteFile;
  217. ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
  218. long size = ftpWebResponse.ContentLength;
  219. ftpResponseStream = ftpWebResponse.GetResponseStream();
  220. streamReader = new StreamReader(ftpResponseStream);
  221. string result = String.Empty;
  222. result = streamReader.ReadToEnd();
  223. success = true;
  224. }
  225. catch (Exception)
  226. {
  227. success = false;
  228. }
  229. finally
  230. {
  231. if (streamReader != null)
  232. {
  233. streamReader.Close();
  234. }
  235. if (ftpResponseStream != null)
  236. {
  237. ftpResponseStream.Close();
  238. }
  239. if (ftpWebResponse != null)
  240. {
  241. ftpWebResponse.Close();
  242. }
  243. }
  244. return success;
  245. }
  246. /// <summary>
  247. /// 文件存在检查
  248. /// </summary>
  249. /// <param name="ftpPath"></param>
  250. /// <param name="ftpName"></param>
  251. /// <returns></returns>
  252. public bool fileCheckExist(string ftpPath, string ftpName)
  253. {
  254. bool success = false;
  255. FtpWebRequest ftpWebRequest = null;
  256. WebResponse webResponse = null;
  257. StreamReader reader = null;
  258. try
  259. {
  260. string url = ftpRootURL + ftpPath;
  261. ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  262. ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  263. ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
  264. ftpWebRequest.KeepAlive = false;
  265. webResponse = ftpWebRequest.GetResponse();
  266. reader = new StreamReader(webResponse.GetResponseStream());
  267. string line = reader.ReadLine();
  268. while (line != null)
  269. {
  270. if (line == ftpName)
  271. {
  272. success = true;
  273. break;
  274. }
  275. line = reader.ReadLine();
  276. }
  277. }
  278. catch (Exception)
  279. {
  280. success = false;
  281. }
  282. finally
  283. {
  284. if (reader != null)
  285. {
  286. reader.Close();
  287. }
  288. if (webResponse != null)
  289. {
  290. webResponse.Close();
  291. }
  292. }
  293. return success;
  294. }
  295. }
  296. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/199805
推荐阅读
相关标签
  

闽ICP备14008679号