FTPWebsiteHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SmartCoalApplication.Base.CommTool
  9. {
  10. /// <summary>
  11. /// FPT帮助类
  12. /// </summary>
  13. public class FTPWebsiteHelper
  14. {
  15. /// <summary>
  16. /// FTP地址 地址 +路径
  17. /// </summary>
  18. public string ftpPath { get; set; }
  19. /// <summary>
  20. /// FTP账号
  21. /// </summary>
  22. public string ftpUserID { get; set; }
  23. /// <summary>
  24. /// FTP密码
  25. /// </summary>
  26. public string ftpPassword { get; set; }
  27. /// <summary>
  28. /// FTP密码
  29. /// </summary>
  30. public string ftpStr { get; set; }
  31. /// <summary>
  32. /// 创建文件夹
  33. /// </summary>
  34. /// <param name="localFile">要上传到FTP服务器的本地文件</param>
  35. /// <param name="ftpPath">FTP地址</param>
  36. public void CreateDictionary(string dateStr)
  37. {
  38. try
  39. {
  40. FtpWebRequest reqFTP;
  41. reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath + "/" + dateStr);// 根据uri创建FtpWebRequest对象
  42. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
  43. reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
  44. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;// 指定执行什么命令
  45. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  46. Stream ftpStream = response.GetResponseStream();
  47. ftpStream.Close();
  48. response.Close();
  49. }
  50. catch (Exception ex)
  51. {
  52. }
  53. }
  54. /// <summary>
  55. /// 上传文件
  56. /// </summary>
  57. /// <param name="localFile">要上传到FTP服务器的本地文件</param>
  58. /// <param name="ftpPath">FTP地址</param>
  59. public void UpLoadFile(string localFile)
  60. {
  61. if (!File.Exists(localFile))
  62. {
  63. return;
  64. }
  65. FileInfo fileInf = new FileInfo(localFile);
  66. FtpWebRequest reqFTP;
  67. string dateStr = DateTime.Now.ToString("yyyyMMdd");
  68. this.ftpStr = dateStr;
  69. CreateDictionary(dateStr);
  70. reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath + "/" + dateStr + "/" + fileInf.Name);// 根据uri创建FtpWebRequest对象
  71. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
  72. reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
  73. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
  74. reqFTP.UseBinary = true;// 指定数据传输类型
  75. reqFTP.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小
  76. int buffLength = 2048;// 缓冲大小设置为2kb
  77. byte[] buff = new byte[buffLength];
  78. int contentLen;
  79. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  80. FileStream fs = fileInf.OpenRead();
  81. try
  82. {
  83. Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流
  84. contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb
  85. while (contentLen != 0)// 流内容没有结束
  86. {
  87. // 把内容从file stream 写入 upload stream
  88. strm.Write(buff, 0, contentLen);
  89. contentLen = fs.Read(buff, 0, buffLength);
  90. }
  91. // 关闭两个流
  92. strm.Close();
  93. fs.Close();
  94. }
  95. catch (Exception ex)
  96. {
  97. throw ex;
  98. }
  99. }
  100. }
  101. }