ftpOperater.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.Windows.Forms;
  8. using UAS_BARCODEIO.Properties;
  9. namespace UAS_BARCODEIO
  10. {
  11. class ftpOperater
  12. {
  13. // public static string FTPAddress = "ftp://172.16.0.20|vsftpd|shenaftp";
  14. public static bool Inner = false;
  15. public static string DownLoadTo = AppDomain.CurrentDomain.BaseDirectory;
  16. private string ftpServerIP;
  17. private string ftpUser;
  18. private string ftpPwd;
  19. public ftpOperater(bool PortModel)
  20. {
  21. //if (!Inner)
  22. //{
  23. // FTPInf = FTPAddress.Split('|');
  24. //}
  25. //else
  26. //{
  27. // FTPInf = FTPAddress.Split('|');
  28. //}
  29. this.ftpServerIP = Settings.Default.ftpaddress;
  30. this.ftpUser = Settings.Default.ftpuser;
  31. this.ftpPwd = Settings.Default.ftppassword;
  32. }
  33. public void UpLoadFile(string filepath, string filename)
  34. {
  35. //上传之前判断文件是否存在
  36. string[] filelist = GetFileList();
  37. if (filelist != null)
  38. for (int i = 0; i < filelist.Length; i++)
  39. {
  40. if (filelist[i] == filename)
  41. {
  42. //string upload = MessageBox.Show("已存在同名文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  43. //if (upload.ToString() != "Yes")
  44. //{
  45. // return;
  46. //}
  47. }
  48. }
  49. FtpWebRequest reqFTP;
  50. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
  51. reqFTP.UseBinary = true;
  52. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  53. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  54. FileInfo file = new FileInfo(filepath + "/" + filename);
  55. const int BufferSize = 2048;
  56. byte[] content = new byte[BufferSize - 1 + 1];
  57. int dataRead;
  58. using (FileStream fs = file.OpenRead())
  59. {
  60. //把上传的文件写入流
  61. using (Stream rs = reqFTP.GetRequestStream())
  62. {
  63. do
  64. {
  65. //每次读文件流的2KB
  66. dataRead = fs.Read(content, 0, BufferSize);
  67. rs.Write(content, 0, dataRead);
  68. } while (!(dataRead < BufferSize));
  69. rs.Close();
  70. }
  71. fs.Close();
  72. }
  73. }
  74. /// <summary>
  75. /// 获取ftp服务器上的文件信息
  76. /// </summary>
  77. /// <returns>存储了所有文件信息的字符串数组</returns>
  78. public string[] GetFileList()
  79. {
  80. string[] downloadFiles;
  81. StringBuilder result = new StringBuilder();
  82. FtpWebRequest reqFTP;
  83. try
  84. {
  85. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/"));
  86. reqFTP.UseBinary = true;
  87. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  88. reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
  89. WebResponse response = reqFTP.GetResponse();
  90. StreamReader reader = new StreamReader(response.GetResponseStream());
  91. string line = reader.ReadLine();
  92. while (line != null)
  93. {
  94. result.Append(line);
  95. result.Append("\n");
  96. line = reader.ReadLine();
  97. }
  98. result.Remove(result.ToString().LastIndexOf('\n'), 1);
  99. reader.Close();
  100. response.Close();
  101. return result.ToString().Split('\n');
  102. }
  103. catch (Exception ex)
  104. {
  105. System.Windows.Forms.MessageBox.Show("获取文件信息失败:" + ex.Message, "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  106. downloadFiles = null;
  107. return downloadFiles;
  108. }
  109. }
  110. /// <summary>
  111. /// 获取FTP上指定文件的大小
  112. /// </summary>
  113. /// <param name="filename">文件名</param>
  114. /// <returns>文件大小</returns>
  115. public long GetFileSize(string filename)
  116. {
  117. FtpWebRequest reqFTP;
  118. long fileSize = 0;
  119. try
  120. {
  121. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
  122. reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
  123. reqFTP.UseBinary = true;
  124. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  125. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  126. Stream ftpStream = response.GetResponseStream();
  127. fileSize = response.ContentLength;
  128. ftpStream.Close();
  129. response.Close();
  130. }
  131. catch (Exception ex)
  132. {
  133. MessageBox.Show("获取文件大小时,出现异常:\n" + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  134. }
  135. return fileSize;
  136. }
  137. /// <summary>
  138. /// 实现ftp下载操作
  139. /// </summary>
  140. /// <param name="fileName">远程文件名</param>
  141. public string Download(string fileName, DateTime time)
  142. {
  143. FtpWebRequest reqFTP;
  144. try
  145. {
  146. FileStream outputStream = new FileStream(DownLoadTo + @"\" + fileName, FileMode.Create);
  147. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
  148. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  149. reqFTP.UseBinary = true;
  150. reqFTP.UsePassive = false;
  151. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  152. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  153. Stream ftpStream = response.GetResponseStream();
  154. long cl = response.ContentLength;
  155. int bufferSize = 4096;
  156. int readCount;
  157. byte[] buffer = new byte[bufferSize];
  158. readCount = ftpStream.Read(buffer, 0, bufferSize);
  159. while (readCount > 0)
  160. {
  161. outputStream.Write(buffer, 0, readCount);
  162. readCount = ftpStream.Read(buffer, 0, bufferSize);
  163. }
  164. ftpStream.Close();
  165. outputStream.Close();
  166. response.Close();
  167. //设置最后修改文件时间为服务器时间
  168. FileInfo f = new FileInfo(DownLoadTo + @"\" + fileName);
  169. f.LastWriteTime = time;
  170. return DownLoadTo + @"\" + fileName;
  171. }
  172. catch (Exception ex)
  173. {
  174. MessageBox.Show(ex.Message);
  175. return "";
  176. }
  177. }
  178. }
  179. }