ftpOperater.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace UAS_LabelMachine
  8. {
  9. class ftpOperater
  10. {
  11. public static string FTPAddress = Properties.Settings.Default.FTPAddress.Split('|')[0];
  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()
  17. {
  18. string[] FTPInf = Properties.Settings.Default.FTPAddress.Split('|');
  19. this.ftpServerIP = FTPInf[0];
  20. this.ftpUser = FTPInf[1];
  21. this.ftpPwd = FTPInf[2];
  22. }
  23. public void UpLoadFile(string filepath, string filename)
  24. {
  25. //上传之前判断文件是否存在
  26. string[] filelist = GetFileList();
  27. if (filelist != null)
  28. for (int i = 0; i < filelist.Length; i++)
  29. {
  30. if (filelist[i] == filename)
  31. {
  32. string upload = MessageBox.Show("已存在同名文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  33. if (upload.ToString() != "Yes")
  34. {
  35. return;
  36. }
  37. }
  38. }
  39. FtpWebRequest reqFTP;
  40. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
  41. reqFTP.UseBinary = true;
  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. 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.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  115. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  116. Stream ftpStream = response.GetResponseStream();
  117. fileSize = response.ContentLength;
  118. ftpStream.Close();
  119. response.Close();
  120. }
  121. catch (Exception ex)
  122. {
  123. MessageBox.Show("获取文件大小时,出现异常:\n" + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  124. }
  125. return fileSize;
  126. }
  127. /// <summary>
  128. /// 实现ftp下载操作
  129. /// </summary>
  130. /// <param name="fileName">远程文件名</param>
  131. public string Download(string fileName)
  132. {
  133. FtpWebRequest reqFTP;
  134. try
  135. {
  136. FileStream outputStream = new FileStream(DownLoadTo + @"\" + fileName, FileMode.Create);
  137. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
  138. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  139. reqFTP.UseBinary = true;
  140. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  141. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  142. Stream ftpStream = response.GetResponseStream();
  143. long cl = response.ContentLength;
  144. int bufferSize = 2048;
  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. return DownLoadTo + @"\" + fileName;
  157. }
  158. catch (Exception ex)
  159. {
  160. MessageBox.Show(ex.Message);
  161. return "";
  162. }
  163. }
  164. }
  165. }