ftpOperater.cs 7.3 KB

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