C#实现FTP上传下载

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Text;

  4. using System.IO;

  5. using System.Data;

  6. using System.Net;

  7. using System.Globalization;

  8.  
  9. namespace FTPConsoleApplication

  10. {

  11. class Program

  12. {

  13. static void Main(string[] args)

  14. {

  15.  
  16. }

  17.  
  18. public static FtpStatusCode UploadFileInFTP(string filename)

  19. {

  20. Stream requestStream = null;

  21. FileStream fileStream = null;

  22. FtpWebResponse uploadResponse = null;

  23. FtpWebRequest uploadRequest = null;

  24. string serverIP;

  25. string userName;

  26. string password;

  27. string uploadurl;

  28.  
  29. try

  30. {

  31. serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];

  32. userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];

  33. password = System.Configuration.ConfigurationManager.AppSettings["Password"];

  34. uploadurl = "ftp://" + serverIP + "/" + Path.GetFileName(filename);

  35. uploadRequest = (FtpWebRequest)WebRequest.Create(uploadurl);

  36. uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;

  37. uploadRequest.Proxy = null;

  38. NetworkCredential nc = new NetworkCredential();

  39. nc.UserName = userName;

  40. nc.Password = password;

  41. uploadRequest.Credentials = nc;

  42. requestStream = uploadRequest.GetRequestStream();

  43. fileStream = File.Open(filename, FileMode.Open);

  44.  
  45. byte[] buffer = new byte[1024];

  46. int bytesRead;

  47.  
  48. while (true)

  49. {

  50. bytesRead = fileStream.Read(buffer, 0, buffer.Length);

  51. if (bytesRead == 0)

  52. {

  53. break;

  54. }

  55. requestStream.Write(buffer, 0, bytesRead);

  56. }

  57.  
  58. requestStream.Close();

  59. uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();

  60. return uploadResponse.StatusCode;

  61. }

  62. catch (Exception e)

  63. {

  64. SystemLog.logger(e.InnerException.Message);

  65. }

  66. finally

  67. {

  68. if (uploadResponse != null)

  69. {

  70. uploadResponse.Close();

  71. }

  72. if (fileStream != null)

  73. {

  74. fileStream.Close();

  75. }

  76. if (requestStream != null)

  77. {

  78. requestStream.Close();

  79. }

  80.  
  81. }

  82. return FtpStatusCode.Undefined;

  83. }

  84.  
  85.  
  86. public static int UploadFtp(string filename)

  87. {

  88. FtpWebRequest reqFTP = null;

  89. string serverIP;

  90. string userName;

  91. string password;

  92. string url;

  93.  
  94. try

  95. {

  96. FileInfo fileInf = new FileInfo(filename);

  97. serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];

  98. userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];

  99. password = System.Configuration.ConfigurationManager.AppSettings["Password"];

  100. url = "ftp://" + serverIP + "/" + Path.GetFileName(filename);

  101.  
  102. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));

  103. reqFTP.Credentials = new NetworkCredential(userName, password);

  104. reqFTP.KeepAlive = false;

  105. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

  106. reqFTP.UseBinary = true;

  107. reqFTP.ContentLength = fileInf.Length;

  108.  
  109. int buffLength = 2048;

  110. byte[] buff = new byte[buffLength];

  111. int contentLen;

  112.  
  113. FileStream fs = fileInf.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

  114.  
  115. Stream strm = reqFTP.GetRequestStream();

  116.  
  117. contentLen = fs.Read(buff, 0, buffLength);

  118.  
  119. while (contentLen != 0)

  120. {

  121.  
  122. strm.Write(buff, 0, contentLen);

  123. contentLen = fs.Read(buff, 0, buffLength);

  124. }

  125.  
  126. strm.Close();

  127. fs.Close();

  128. return 0;

  129. }

  130. catch (Exception ex)

  131. {

  132. if (reqFTP != null)

  133. {

  134. reqFTP.Abort();

  135. }

  136. SystemLog.logger(ex.InnerException.Message);

  137. return -2;

  138. }

  139. }

  140.  
  141.  
  142. public static int DownloadFtp(string filename)

  143. {

  144. FtpWebRequest reqFTP;

  145. string serverIP;

  146. string userName;

  147. string password;

  148. string url;

  149.  
  150. try

  151. {

  152. serverIP = System.Configuration.ConfigurationManager.AppSettings["FTPServerIP"];

  153. userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];

  154. password = System.Configuration.ConfigurationManager.AppSettings["Password"];

  155. url = "ftp://" + serverIP + "/" + Path.GetFileName(filename);

  156.  
  157. FileStream outputStream = new FileStream(filename, FileMode.Create);

  158. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));

  159. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

  160. reqFTP.UseBinary = true;

  161. reqFTP.KeepAlive = false;

  162. reqFTP.Credentials = new NetworkCredential(userName, password);

  163. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

  164. Stream ftpStream = response.GetResponseStream();

  165. long cl = response.ContentLength;

  166. int bufferSize = 2048;

  167. int readCount;

  168. byte[] buffer = new byte[bufferSize];

  169. readCount = ftpStream.Read(buffer, 0, bufferSize);

  170. while (readCount > 0)

  171. {

  172. outputStream.Write(buffer, 0, readCount);

  173. readCount = ftpStream.Read(buffer, 0, bufferSize);

  174. }

  175. ftpStream.Close();

  176. outputStream.Close();

  177. response.Close();

  178. return 0;

  179. }

  180. catch (Exception ex)

  181. {

  182. SystemLog.logger(ex.InnerException.Message);

  183. return -2;

  184. }

  185. }

  186.  
  187.  
  188. public class SystemLog

  189. {

  190.  
  191. public static bool logger(string message)

  192. {

  193. try

  194. {

  195. DateTime timeNow = DateTime.Now;

  196. string filename = System.Configuration.ConfigurationManager.AppSettings["LogPath"];

  197. string logSwitch = System.Configuration.ConfigurationManager.AppSettings["LogSwitch"];

  198. if (logSwitch == "1")

  199. {

  200. Encoding encoding = Encoding.GetEncoding("gb2312");

  201. byte[] info = encoding.GetBytes("[ " + timeNow.ToString("yyyy-MM-dd HH:mm:ss") + " ] " + message + "\n"); //转换编码成字节串

  202.  
  203. if (!filename.EndsWith(Path.DirectorySeparatorChar.ToString()))

  204. {

  205. filename = filename + Path.DirectorySeparatorChar.ToString();

  206. }

  207.  
  208. using (FileStream fs = System.IO.File.Open(filename + timeNow.ToString("yyyy_MM_dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite))

  209. {

  210. fs.Write(info, 0, info.Length);

  211. //以ASCII方式编写

  212. using (StreamWriter w = new StreamWriter(fs, Encoding.ASCII))

  213. {

  214. w.Flush();

  215. w.Close();

  216. }

  217.  
  218. fs.Close();

  219. }

  220. }

  221. return true;

  222. }

  223. catch (Exception e)

  224. {

  225. return false;

  226. }

  227. }

  228. }

  229.  
  230. }

  231. }

 

开启Windows 7 下的FTP服务

1、

2、

3、

4、

5、

6、

7、

8、

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值