VSFTPHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MeterPlugInLibrary
  11. {
  12. public class VSFTPHelper
  13. {
  14. #region 字段或属性
  15. public string IP { get; set; }
  16. public string UserName { get; set; }
  17. public string Password { get; set; }
  18. public bool Passive { get; set; }
  19. public bool Binary { get; set; }
  20. #endregion
  21. #region 构造
  22. /// <summary>
  23. /// 构造
  24. /// </summary>
  25. public VSFTPHelper()
  26. {
  27. IP = AppConfigCache.ftpIp;
  28. Passive = true;
  29. UserName = "";
  30. Password = "";
  31. Binary = true;
  32. WriteLog(string.Format("VSFTP参数设定: {0} {1} {2}", IP, UserName, Password));
  33. }
  34. public VSFTPHelper(string ip,string userNmae, string password )
  35. {
  36. IP = ip;
  37. Passive = true;
  38. UserName = userNmae;
  39. Password = password;
  40. Binary = true;
  41. WriteLog(string.Format("VSFTP参数设定: {0} {1} {2}", IP, UserName, Password));
  42. }
  43. #endregion
  44. #region 创建文件夹
  45. /// <summary>
  46. /// 创建一个目录
  47. /// </summary>
  48. public void CreateDirectory(string filesUrl)
  49. {
  50. try
  51. {
  52. Uri uri = new Uri("ftp://" + IP + AppConfigCache.ftpPath + filesUrl);
  53. List<string> list = new List<string>();
  54. FtpWebRequest req = (FtpWebRequest)WebRequest.Create(uri); //
  55. req.Credentials = new NetworkCredential(UserName, Password);
  56. req.Method = WebRequestMethods.Ftp.MakeDirectory;
  57. req.UseBinary = Binary;
  58. FtpWebResponse res = (FtpWebResponse)req.GetResponse();
  59. //GetFileList(filesUrl); //假如创建不成功,反复调用。。。
  60. }
  61. catch (Exception exp)
  62. {
  63. //WriteLog("FTP图片上传失败CreateDirectory创建一个目录!" + exp.Message);
  64. }
  65. }
  66. /// <summary>
  67. /// 获取FTP文件列表 /jldata/ftppicture/
  68. /// </summary>
  69. /// <returns></returns>
  70. public List<String> GetFileList(string filesUrl)
  71. {
  72. List<string> list = new List<string>();
  73. try
  74. {
  75. FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + IP + AppConfigCache.ftpPath + "/" + filesUrl)); //
  76. req.Credentials = new NetworkCredential(UserName, Password);
  77. req.Method = WebRequestMethods.Ftp.ListDirectory;
  78. req.UseBinary = Binary;
  79. req.UsePassive = Passive;
  80. using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
  81. {
  82. using (StreamReader sr = new StreamReader(res.GetResponseStream()))
  83. {
  84. string s;
  85. while ((s = sr.ReadLine()) != null)
  86. {
  87. list.Add(s);
  88. }
  89. }
  90. }
  91. //这里的list是文件夹下的文件信息
  92. return list;
  93. }
  94. catch (Exception exp)
  95. {
  96. CreateDirectory(filesUrl);
  97. return list;
  98. }
  99. }
  100. #endregion
  101. #region 上传文件
  102. /// <summary>
  103. /// FTP图片上传
  104. /// </summary>
  105. /// <param name="localPath">本地文件夹路径</param>
  106. public bool FtpUp(string localPath)
  107. {
  108. try
  109. {
  110. //创建文件夹
  111. CreateDirectory(DateTime.Now.ToString("yyyy-MM-dd"));
  112. //上传文件
  113. UploadFile(localPath);
  114. //未截图成功的,也上传一个图片
  115. //File.Copy(strFilePath + "\\image\\null.jpg", strPathImage);
  116. //UploadFile(strPathImage);
  117. return true;
  118. }
  119. catch (Exception exp)
  120. {
  121. WriteLog("FTP图片上传失败!" + exp.Message);
  122. return false;
  123. }
  124. }
  125. /// <summary>
  126. /// 上传文件
  127. /// </summary>
  128. /// <param name="localPath"></param>
  129. private void UploadFile(string localPath)
  130. {
  131. FileInfo fi = new FileInfo(localPath);
  132. FileStream fs = fi.OpenRead();
  133. long length = fs.Length;
  134. FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + IP + AppConfigCache.ftpPath + DateTime.Now.ToString("yyyy-MM-dd") + "/" + fi.Name);
  135. req.Credentials = new NetworkCredential(UserName, Password);
  136. req.Method = WebRequestMethods.Ftp.UploadFile;
  137. req.UseBinary = Binary;
  138. req.ContentLength = length;
  139. req.Timeout = 10 * 1000;
  140. Stream stream = req.GetRequestStream();
  141. int BufferLength = 2048; //2K
  142. byte[] b = new byte[BufferLength];
  143. int i;
  144. while ((i = fs.Read(b, 0, BufferLength)) > 0)
  145. {
  146. stream.Write(b, 0, i);
  147. }
  148. stream.Close();
  149. stream.Dispose();
  150. fs.Close();
  151. }
  152. #endregion
  153. #region 写日志
  154. private static void WriteLog(string str)
  155. {
  156. string m_szRunPath;
  157. try
  158. {
  159. m_szRunPath = System.Environment.CurrentDirectory;
  160. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  161. {
  162. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  163. }
  164. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  165. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  166. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  167. {
  168. Directory.CreateDirectory(strPathFile);
  169. }
  170. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\FTP_" + strDate + ".log", true);
  171. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  172. tw.WriteLine(str);
  173. tw.WriteLine("\r\n");
  174. tw.Close();
  175. }
  176. catch { }
  177. }
  178. #endregion
  179. }
  180. }