VSFTPHelper.cs 6.0 KB

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