ftpOperater.cs 7.0 KB

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