FtpAux2.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.IO;
  5. using System.Threading;
  6. namespace Common
  7. {
  8. public class FtpAux2
  9. {
  10. private string strFilePath = System.Environment.CurrentDirectory;//路径
  11. private string _remote_path = "";
  12. //private string FtpIP = "192.1.3.240";//192.1.3.240
  13. public string IP { get; set; }
  14. public string RemotePath
  15. {
  16. get { return _remote_path; }
  17. set
  18. {
  19. _remote_path = value;
  20. if (!_remote_path.EndsWith("/")) _remote_path += "/";
  21. }
  22. }
  23. public string UserName { get; set; }
  24. public string Password { get; set; }
  25. public string LocalPath { get; set; }
  26. public bool Passive { get; set; }
  27. public bool Binary { get; set; }
  28. public FtpAux2()
  29. {
  30. IP = AppConfigCache.ftpIp;
  31. Passive = true;
  32. _remote_path = "";
  33. LocalPath = "c:/";
  34. UserName = AppConfigCache.ftpUid;
  35. Password = AppConfigCache.ftpPwd;
  36. Binary = true;
  37. }
  38. public void DownLoadFile(string filename)
  39. {
  40. Uri uri = new Uri("ftp://" + IP + RemotePath + filename);
  41. FtpWebRequest req = (FtpWebRequest)WebRequest.Create(uri);
  42. req.Method = WebRequestMethods.Ftp.DownloadFile;
  43. req.UseBinary = Binary;
  44. req.UsePassive = Passive;
  45. req.Credentials = new NetworkCredential(UserName, Password);
  46. using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
  47. {
  48. string localfile = Path.Combine(LocalPath, filename);
  49. FileStream fs = new FileStream(localfile, FileMode.Create, FileAccess.Write);
  50. int buffer = 1024; //1K缓冲
  51. byte[] b = new byte[buffer];
  52. int i = 0;
  53. Stream stream = res.GetResponseStream();
  54. while ((i = stream.Read(b, 0, buffer)) > 0)
  55. {
  56. fs.Write(b, 0, i);
  57. }
  58. fs.Flush();
  59. fs.Close();
  60. }
  61. }
  62. /// <summary>
  63. /// 创建一个目录
  64. /// </summary>
  65. public void CreateDirectory()
  66. {
  67. try
  68. {
  69. FileInfo fi = new FileInfo(RemotePath);
  70. Uri uri = new Uri("ftp://" + IP + RemotePath);
  71. List<string> list = new List<string>();
  72. FtpWebRequest req = (FtpWebRequest)WebRequest.Create(uri); //
  73. req.Credentials = new NetworkCredential(UserName, Password);
  74. req.Method = WebRequestMethods.Ftp.MakeDirectory;
  75. req.UseBinary = Binary;
  76. FtpWebResponse res = (FtpWebResponse)req.GetResponse();
  77. GetFileList();
  78. }
  79. catch (Exception exp)
  80. {
  81. }
  82. }
  83. /// <summary>
  84. /// 获取FTP文件列表 /jldata/ftppicture/
  85. /// </summary>
  86. /// <returns></returns>
  87. public List<String> GetFileList()
  88. {
  89. List<string> list = new List<string>();
  90. try
  91. {
  92. FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + IP + RemotePath)); //
  93. req.Credentials = new NetworkCredential(UserName, Password);
  94. req.Method = WebRequestMethods.Ftp.ListDirectory;
  95. req.UseBinary = Binary;
  96. req.UsePassive = Passive;
  97. using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
  98. {
  99. using (StreamReader sr = new StreamReader(res.GetResponseStream()))
  100. {
  101. string s;
  102. while ((s = sr.ReadLine()) != null)
  103. {
  104. list.Add(s);
  105. }
  106. }
  107. }
  108. return list;
  109. }
  110. catch (Exception exp)
  111. {
  112. CreateDirectory();
  113. return list;
  114. }
  115. }
  116. public void UploadFile(string localFile)
  117. {
  118. FileInfo fi = new FileInfo(localFile);
  119. FileStream fs = fi.OpenRead();
  120. long length = fs.Length;
  121. FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + IP + RemotePath + fi.Name);
  122. req.Credentials = new NetworkCredential(UserName, Password);
  123. req.Method = WebRequestMethods.Ftp.UploadFile;
  124. req.UseBinary = Binary;
  125. req.ContentLength = length;
  126. req.Timeout = 10 * 1000;
  127. Stream stream = req.GetRequestStream();
  128. int BufferLength = 2048; //2K
  129. byte[] b = new byte[BufferLength];
  130. int i;
  131. while ((i = fs.Read(b, 0, BufferLength)) > 0)
  132. {
  133. stream.Write(b, 0, i);
  134. }
  135. stream.Close();
  136. stream.Dispose();
  137. fs.Close();
  138. }
  139. public void UploadFile(string fileName, byte[] data)
  140. {
  141. long length = data.Length;
  142. FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + IP + RemotePath + fileName);
  143. req.Credentials = new NetworkCredential(UserName, Password);
  144. req.Method = WebRequestMethods.Ftp.UploadFile;
  145. req.UseBinary = Binary;
  146. req.ContentLength = length;
  147. req.Timeout = 10 * 1000;
  148. Stream stream = req.GetRequestStream();
  149. stream.Write(data, 0, data.Length);
  150. stream.Close();
  151. stream.Dispose();
  152. }
  153. /// <summary>
  154. /// FTP图片上传
  155. /// </summary>
  156. /// <param name="strPath"></param>
  157. /// <param name="strWgtNo"></param>
  158. public bool FtpUp(string strPath, string strWgtNo)
  159. {
  160. try
  161. {
  162. Thread.Sleep(500);
  163. LocalPath = "d:/";
  164. UserName = "ftpuser";
  165. Password = "admin_2019";//ftpuser admin_2019
  166. //ftp.RemotePath = "//home/xgkftp/image/" + DateTime.Now.ToString("yyyy-MM-dd");///home/xgkftp/image/
  167. RemotePath = "//Image/mcms/testCAR/" + DateTime.Now.ToString("yyyy-MM-dd");//RemotePath
  168. GetFileList();
  169. string[] arrayFileImage = new string[6];
  170. for (int i = 1; i < 8; i++)
  171. {
  172. string strPathImage = strPath + "_" + i.ToString() + ".jpg";
  173. if (File.Exists(strPathImage))
  174. {//存在的图片进行上传操作
  175. UploadFile(strPathImage);
  176. WriteLog("图片上传后,进行删除操作,图片路径:" + strPathImage);
  177. File.Delete(strPathImage);
  178. }
  179. //else
  180. //{//未截图成功的,也上传一个图片
  181. // File.Copy(strFilePath + "\\image\\null.jpg", strPathImage);
  182. // UploadFile(strPathImage);
  183. //}
  184. arrayFileImage[i - 1] = RemotePath + strWgtNo + "_" + i.ToString() + ".jpg";
  185. }
  186. return true;
  187. }
  188. catch (Exception exp)
  189. {
  190. WriteLog("FTP图片上传失败!" + strPath + exp.Message + exp.StackTrace);
  191. return false;
  192. }
  193. }
  194. private void WriteLog(string str)
  195. {
  196. if (System.IO.Directory.Exists(strFilePath + "\\log") == false)
  197. {
  198. System.IO.Directory.CreateDirectory(strFilePath + "\\log");
  199. }
  200. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  201. System.IO.TextWriter tw = new System.IO.StreamWriter(strFilePath + "\\log\\" + "FTP_" + strDate + ".log", true);
  202. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  203. tw.WriteLine(str);
  204. tw.WriteLine("\r\n");
  205. tw.Close();
  206. }
  207. }
  208. }