ftpOperater.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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()
  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.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. WebResponse response = reqFTP.GetResponse();
  79. StreamReader reader = new StreamReader(response.GetResponseStream());
  80. string line = reader.ReadLine();
  81. while (line != null)
  82. {
  83. result.Append(line);
  84. result.Append("\n");
  85. line = reader.ReadLine();
  86. }
  87. result.Remove(result.ToString().LastIndexOf('\n'), 1);
  88. reader.Close();
  89. response.Close();
  90. return result.ToString().Split('\n');
  91. }
  92. catch (Exception ex)
  93. {
  94. System.Windows.Forms.MessageBox.Show("获取文件信息失败:" + ex.Message, "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  95. downloadFiles = null;
  96. return downloadFiles;
  97. }
  98. }
  99. /// <summary>
  100. /// 获取FTP上指定文件的大小
  101. /// </summary>
  102. /// <param name="filename">文件名</param>
  103. /// <returns>文件大小</returns>
  104. public long GetFileSize(string filename)
  105. {
  106. FtpWebRequest reqFTP;
  107. long fileSize = 0;
  108. try
  109. {
  110. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
  111. reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
  112. reqFTP.UseBinary = true;
  113. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  114. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  115. Stream ftpStream = response.GetResponseStream();
  116. fileSize = response.ContentLength;
  117. ftpStream.Close();
  118. response.Close();
  119. }
  120. catch (Exception ex)
  121. {
  122. MessageBox.Show("获取文件大小时,出现异常:\n" + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  123. }
  124. return fileSize;
  125. }
  126. /// <summary>
  127. /// 实现ftp下载操作
  128. /// </summary>
  129. /// <param name="fileName">远程文件名</param>
  130. public string Download(string fileName, DateTime time)
  131. {
  132. FtpWebRequest reqFTP;
  133. try
  134. {
  135. FileStream outputStream = new FileStream(DownLoadTo + @"\" + fileName, FileMode.Create);
  136. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
  137. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  138. reqFTP.UseBinary = true;
  139. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  140. reqFTP.UsePassive = true;
  141. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  142. Stream ftpStream = response.GetResponseStream();
  143. long cl = response.ContentLength;
  144. int bufferSize = 4096;
  145. int readCount;
  146. byte[] buffer = new byte[bufferSize];
  147. readCount = ftpStream.Read(buffer, 0, bufferSize);
  148. while (readCount > 0)
  149. {
  150. outputStream.Write(buffer, 0, readCount);
  151. readCount = ftpStream.Read(buffer, 0, bufferSize);
  152. }
  153. ftpStream.Close();
  154. outputStream.Close();
  155. response.Close();
  156. //设置最后修改文件时间为服务器时间
  157. FileInfo f = new FileInfo(DownLoadTo + @"\" + fileName);
  158. f.LastWriteTime = time;
  159. return DownLoadTo + @"\" + fileName;
  160. }
  161. catch (Exception ex)
  162. {
  163. MessageBox.Show(ex.Message);
  164. return "";
  165. }
  166. }
  167. }
  168. }