ftpOperater.cs 7.1 KB

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