ftpOperater.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using UAS_LabelMachine.Entity;
  7. namespace UAS_LabelMachine
  8. {
  9. class ftpOperater
  10. {
  11. public static string FTPAddress = DataHelper.FTPAddress.Split('|')[0];
  12. public static bool Inner = false;
  13. public static string DownLoadTo = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":\" + @"打印标签\";
  14. private string ftpServerIP;
  15. private string ftpUser;
  16. private string ftpPwd;
  17. public ftpOperater(bool PortModel)
  18. {
  19. string[] FTPInf;
  20. if (!Inner)
  21. {
  22. FTPInf = DataHelper.FTPAddress.Split('|');
  23. }
  24. else
  25. {
  26. FTPInf = DataHelper.InnerFTPAddress.Split('|');
  27. }
  28. this.ftpServerIP = FTPInf[0];
  29. this.ftpUser = FTPInf[1];
  30. this.ftpPwd = FTPInf[2];
  31. }
  32. public void UpLoadFile(string filepath, string filename)
  33. {
  34. //上传之前判断文件是否存在
  35. string[] filelist = GetFileList();
  36. if (filelist != null)
  37. for (int i = 0; i < filelist.Length; i++)
  38. {
  39. if (filelist[i] == filename)
  40. {
  41. //string upload = MessageBox.Show("已存在同名文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  42. //if (upload.ToString() != "Yes")
  43. //{
  44. // return;
  45. //}
  46. }
  47. }
  48. FtpWebRequest reqFTP;
  49. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
  50. reqFTP.UseBinary = true;
  51. reqFTP.UsePassive = SystemInf.FTPModel;
  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. reqFTP.UsePassive = SystemInf.FTPModel;
  90. WebResponse response = reqFTP.GetResponse();
  91. StreamReader reader = new StreamReader(response.GetResponseStream());
  92. string line = reader.ReadLine();
  93. while (line != null)
  94. {
  95. result.Append(line);
  96. result.Append("\n");
  97. line = reader.ReadLine();
  98. }
  99. result.Remove(result.ToString().LastIndexOf('\n'), 1);
  100. reader.Close();
  101. response.Close();
  102. return result.ToString().Split('\n');
  103. }
  104. catch (Exception ex)
  105. {
  106. System.Windows.Forms.MessageBox.Show("获取文件信息失败:" + ex.Message, "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  107. downloadFiles = null;
  108. return downloadFiles;
  109. }
  110. }
  111. /// <summary>
  112. /// 获取FTP上指定文件的大小
  113. /// </summary>
  114. /// <param name="filename">文件名</param>
  115. /// <returns>文件大小</returns>
  116. public long GetFileSize(string filename)
  117. {
  118. FtpWebRequest reqFTP;
  119. long fileSize = 0;
  120. try
  121. {
  122. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
  123. reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
  124. reqFTP.UseBinary = true;
  125. reqFTP.UsePassive = SystemInf.FTPModel;
  126. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  127. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  128. Stream ftpStream = response.GetResponseStream();
  129. fileSize = response.ContentLength;
  130. ftpStream.Close();
  131. response.Close();
  132. }
  133. catch (Exception ex)
  134. {
  135. MessageBox.Show("获取文件大小时,出现异常:\n" + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  136. }
  137. return fileSize;
  138. }
  139. /// <summary>
  140. /// 实现ftp下载操作
  141. /// </summary>
  142. /// <param name="fileName">远程文件名</param>
  143. public string Download(string fileName, DateTime time)
  144. {
  145. FtpWebRequest reqFTP;
  146. try
  147. {
  148. FileStream outputStream = new FileStream(DownLoadTo + @"\" + fileName, FileMode.Create);
  149. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
  150. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  151. reqFTP.UseBinary = true;
  152. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  153. reqFTP.UsePassive = SystemInf.FTPModel;
  154. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  155. Stream ftpStream = response.GetResponseStream();
  156. long cl = response.ContentLength;
  157. int bufferSize = 4096;
  158. int readCount;
  159. byte[] buffer = new byte[bufferSize];
  160. readCount = ftpStream.Read(buffer, 0, bufferSize);
  161. while (readCount > 0)
  162. {
  163. outputStream.Write(buffer, 0, readCount);
  164. readCount = ftpStream.Read(buffer, 0, bufferSize);
  165. }
  166. ftpStream.Close();
  167. outputStream.Close();
  168. response.Close();
  169. //设置最后修改文件时间为服务器时间
  170. FileInfo f = new FileInfo(DownLoadTo + @"\" + fileName);
  171. f.LastWriteTime = time;
  172. return DownLoadTo + @"\" + fileName;
  173. }
  174. catch (Exception ex)
  175. {
  176. MessageBox.Show(ex.Message);
  177. return "";
  178. }
  179. }
  180. }
  181. }