2e3eb3b858b19ec711cb643d471f86b7d218f08f.svn-base 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Net;
  6. namespace Core.Mes.ServerFrameWork
  7. {
  8. public delegate void Action<T1, T2>(T1 _t1, T2 _t2);
  9. public static class FtpHelper
  10. {
  11. public static string FtpUserID = string.Empty;
  12. public static string FtpPassword = string.Empty;
  13. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, Action<int, int> updateProgress)
  14. {
  15. Stream stream = null;
  16. FtpWebResponse ftpWebResponse = null;
  17. FileStream fileStream = null;
  18. try
  19. {
  20. fileStream = new FileStream(localFileName, FileMode.Create);
  21. Uri requestUri = new Uri(remoteFileName);
  22. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
  23. ftpWebRequest.UseBinary = true;
  24. FtpWebRequest ftpWebRequest2 = (FtpWebRequest)WebRequest.Create(requestUri);
  25. ftpWebRequest2.UseBinary = true;
  26. ftpWebRequest2.KeepAlive = false;
  27. if (ifCredential)
  28. {
  29. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  30. ftpWebRequest2.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  31. }
  32. ftpWebRequest.Method = "SIZE";
  33. FtpWebResponse ftpWebResponse2 = (FtpWebResponse)ftpWebRequest.GetResponse();
  34. long contentLength = ftpWebResponse2.ContentLength;
  35. ftpWebResponse2.Close();
  36. ftpWebRequest2.Method = "RETR";
  37. ftpWebResponse = (FtpWebResponse)ftpWebRequest2.GetResponse();
  38. stream = ftpWebResponse.GetResponseStream();
  39. if (updateProgress != null)
  40. updateProgress(0, (int)contentLength);
  41. long num = 0L;
  42. int num2 = 2048;
  43. byte[] buffer = new byte[num2];
  44. for (int num3 = stream.Read(buffer, 0, num2); num3 > 0; num3 = stream.Read(buffer, 0, num2))
  45. {
  46. num = num3 + num;
  47. fileStream.Write(buffer, 0, num3);
  48. if (updateProgress != null)
  49. updateProgress((int)num, (int)contentLength);
  50. }
  51. stream.Close();
  52. fileStream.Close();
  53. ftpWebResponse.Close();
  54. return true;
  55. }
  56. catch (Exception)
  57. {
  58. return false;
  59. }
  60. finally
  61. {
  62. stream.Close();
  63. fileStream.Close();
  64. ftpWebResponse.Close();
  65. }
  66. }
  67. public static bool FtpBrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress)
  68. {
  69. Stream stream = null;
  70. FtpWebResponse ftpWebResponse = null;
  71. FileStream fileStream = null;
  72. try
  73. {
  74. fileStream = new FileStream(localFileName, FileMode.Append);
  75. Uri requestUri = new Uri(remoteFileName);
  76. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
  77. ftpWebRequest.UseBinary = true;
  78. ftpWebRequest.ContentOffset = size;
  79. FtpWebRequest ftpWebRequest2 = (FtpWebRequest)WebRequest.Create(requestUri);
  80. ftpWebRequest2.UseBinary = true;
  81. ftpWebRequest2.KeepAlive = false;
  82. ftpWebRequest2.ContentOffset = size;
  83. if (ifCredential)
  84. {
  85. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  86. ftpWebRequest2.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  87. }
  88. ftpWebRequest.Method = "SIZE";
  89. FtpWebResponse ftpWebResponse2 = (FtpWebResponse)ftpWebRequest.GetResponse();
  90. long contentLength = ftpWebResponse2.ContentLength;
  91. ftpWebResponse2.Close();
  92. ftpWebRequest2.Method = "RETR";
  93. ftpWebResponse = (FtpWebResponse)ftpWebRequest2.GetResponse();
  94. stream = ftpWebResponse.GetResponseStream();
  95. if (updateProgress != null)
  96. updateProgress(0, (int)contentLength);
  97. long num = 0L;
  98. int num2 = 2048;
  99. byte[] buffer = new byte[num2];
  100. for (int num3 = stream.Read(buffer, 0, num2); num3 > 0; num3 = stream.Read(buffer, 0, num2))
  101. {
  102. num = num3 + num;
  103. fileStream.Write(buffer, 0, num3);
  104. if (updateProgress != null)
  105. updateProgress((int)num, (int)contentLength);
  106. }
  107. stream.Close();
  108. fileStream.Close();
  109. ftpWebResponse.Close();
  110. return true;
  111. }
  112. catch (Exception)
  113. {
  114. return false;
  115. }
  116. finally
  117. {
  118. stream.Close();
  119. fileStream.Close();
  120. ftpWebResponse.Close();
  121. }
  122. }
  123. public static bool FtpDownload(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress)
  124. {
  125. if (brokenOpen)
  126. {
  127. try
  128. {
  129. long size = 0L;
  130. if (File.Exists(localFileName))
  131. {
  132. using (FileStream fileStream = new FileStream(localFileName, FileMode.Open))
  133. {
  134. size = fileStream.Length;
  135. }
  136. }
  137. return FtpBrokenDownload(remoteFileName, localFileName, ifCredential, size, updateProgress);
  138. }
  139. catch
  140. {
  141. throw;
  142. }
  143. }
  144. return FtpDownload(remoteFileName, localFileName, ifCredential, updateProgress);
  145. }
  146. public static bool FtpUploadFile(string localFullPathName, string remotePath, Action<int, int> updateProgress)
  147. {
  148. Stream stream = null;
  149. FtpWebResponse ftpWebResponse = null;
  150. FileStream fileStream = null;
  151. try
  152. {
  153. FileInfo fileInfo = new FileInfo(localFullPathName);
  154. Uri requestUri = new Uri(remotePath);
  155. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri);
  156. ftpWebRequest.KeepAlive = false;
  157. ftpWebRequest.UseBinary = true;
  158. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  159. ftpWebRequest.Method = "STOR";
  160. ftpWebRequest.ContentLength = fileInfo.Length;
  161. ftpWebResponse = (ftpWebRequest.GetResponse() as FtpWebResponse);
  162. ftpWebRequest.ContentLength = fileInfo.Length;
  163. int num = 1024;
  164. byte[] buffer = new byte[num];
  165. fileStream = fileInfo.OpenRead();
  166. stream = ftpWebRequest.GetRequestStream();
  167. int num2 = fileStream.Read(buffer, 0, num);
  168. int num3 = (int)fileInfo.Length;
  169. if (updateProgress != null)
  170. updateProgress(0, num3);
  171. int num4 = 0;
  172. while (num2 != 0)
  173. {
  174. num4 = num2 + num4;
  175. stream.Write(buffer, 0, num2);
  176. if (updateProgress != null)
  177. updateProgress(num4, num3);
  178. num2 = fileStream.Read(buffer, 0, num);
  179. }
  180. stream.Close();
  181. fileStream.Close();
  182. ftpWebResponse.Close();
  183. return true;
  184. }
  185. catch (Exception)
  186. {
  187. return false;
  188. }
  189. finally
  190. {
  191. fileStream.Close();
  192. stream.Close();
  193. ftpWebResponse.Close();
  194. }
  195. }
  196. public static bool FtpUploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress)
  197. {
  198. if (remoteFilepath == null)
  199. {
  200. remoteFilepath = "";
  201. }
  202. string empty = string.Empty;
  203. bool result = true;
  204. FileInfo fileInfo = new FileInfo(localFullPath);
  205. long length = fileInfo.Length;
  206. if (fileInfo.Name.IndexOf("#") == -1)
  207. {
  208. empty = RemoveSpaces(fileInfo.Name);
  209. }
  210. else
  211. {
  212. empty = fileInfo.Name.Replace("#", "#");
  213. empty = RemoveSpaces(empty);
  214. }
  215. long fileSize = GetFileSize(empty, remoteFilepath);
  216. if (fileSize >= length)
  217. {
  218. return false;
  219. }
  220. long num = fileSize;
  221. if (updateProgress != null)
  222. updateProgress((int)fileSize, (int)length);
  223. string uriString = (remoteFilepath.Length != 0) ? (remoteFilepath + "/" + remoteFilepath + "/" + empty) : (remoteFilepath + "/" + empty);
  224. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(new Uri(uriString));
  225. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  226. ftpWebRequest.KeepAlive = false;
  227. ftpWebRequest.Method = "APPE";
  228. ftpWebRequest.UseBinary = true;
  229. ftpWebRequest.ContentLength = fileInfo.Length;
  230. int num2 = 2048;
  231. byte[] buffer = new byte[num2];
  232. FileStream fileStream = fileInfo.OpenRead();
  233. Stream stream = null;
  234. try
  235. {
  236. stream = ftpWebRequest.GetRequestStream();
  237. fileStream.Seek(fileSize, SeekOrigin.Begin);
  238. int num3 = fileStream.Read(buffer, 0, num2);
  239. while (num3 != 0)
  240. {
  241. stream.Write(buffer, 0, num3);
  242. num3 = fileStream.Read(buffer, 0, num2);
  243. num += num3;
  244. if (updateProgress != null)
  245. updateProgress((int)num, (int)length);
  246. }
  247. stream.Close();
  248. fileStream.Close();
  249. }
  250. catch
  251. {
  252. result = false;
  253. throw;
  254. }
  255. finally
  256. {
  257. fileStream.Close();
  258. stream.Close();
  259. }
  260. return result;
  261. }
  262. private static string RemoveSpaces(string str)
  263. {
  264. string text = "";
  265. CharEnumerator enumerator = str.GetEnumerator();
  266. while (enumerator.MoveNext())
  267. {
  268. byte[] array = new byte[1];
  269. array = Encoding.ASCII.GetBytes(enumerator.Current.ToString());
  270. int num = array[0];
  271. if (num != 32)
  272. {
  273. text += enumerator.Current.ToString();
  274. }
  275. }
  276. string text2 = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
  277. return text.Split('.')[text.Split('.').Length - 2] + "." + text.Split('.')[text.Split('.').Length - 1];
  278. }
  279. public static long GetFileSize(string filename, string remoteFilepath)
  280. {
  281. try
  282. {
  283. if (remoteFilepath.Length == 0)
  284. {
  285. return 0L;
  286. }
  287. FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(remoteFilepath);
  288. ftpWebRequest.KeepAlive = false;
  289. ftpWebRequest.UseBinary = true;
  290. ftpWebRequest.Credentials = new NetworkCredential(FtpUserID, FtpPassword);
  291. ftpWebRequest.Method = "SIZE";
  292. FtpWebResponse ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
  293. return ftpWebResponse.ContentLength;
  294. }
  295. catch
  296. {
  297. return 0L;
  298. }
  299. }
  300. }
  301. }