BgFtpOperater.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. namespace UAS_MES_NEW.PublicMethod
  8. {
  9. internal class BgFtpOperater
  10. {
  11. private string ftpServerIP;
  12. private string ftpUser;
  13. private string ftpPwd;
  14. public BgFtpOperater()
  15. {
  16. //百岗ftp
  17. string[] FTPInf = "ftp://10.8.0.208:21//mes/TestData|vsftpd|Az789***".Split('|');
  18. this.ftpServerIP = FTPInf[0];
  19. this.ftpUser = FTPInf[1];
  20. this.ftpPwd = FTPInf[2];
  21. //string FTPInf = Properties.Settings.Default.Properties["FTPAddress"].DefaultValue.ToString();
  22. //连接共享文件夹
  23. //status = BaseUtil.connectState(FTPInf);
  24. }
  25. public string UpLoadFile(string filepath, string filename, string UploadFolder)
  26. {
  27. try
  28. {
  29. FtpWebRequest reqFTP;
  30. if (FtpCheckDirectoryExist(UploadFolder) == "")
  31. {
  32. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + UploadFolder + filename));
  33. reqFTP.UseBinary = true;
  34. reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  35. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  36. FileInfo file = new FileInfo(filepath + "/" + filename);
  37. const int BufferSize = 2048;
  38. byte[] content = new byte[BufferSize - 1 + 1];
  39. int dataRead;
  40. using (FileStream fs = file.OpenRead())
  41. {
  42. //把上传的文件写入流
  43. using (Stream rs = reqFTP.GetRequestStream())
  44. {
  45. do
  46. {
  47. //每次读文件流的2KB
  48. dataRead = fs.Read(content, 0, BufferSize);
  49. rs.Write(content, 0, dataRead);
  50. } while (!(dataRead < BufferSize));
  51. rs.Close();
  52. }
  53. fs.Close();
  54. fs.Dispose();
  55. }
  56. file.Delete();
  57. }
  58. //Thread.Sleep(1000);
  59. }
  60. catch (Exception ex)
  61. {
  62. return ex.Message;
  63. }
  64. return "";
  65. //File.Delete(filepath + "/" + filename);
  66. }
  67. public string FtpCheckDirectoryExist(string destFilePath)
  68. {
  69. string fullDir = FtpParseDirectory(destFilePath);
  70. string[] dirs = fullDir.Split('/');
  71. string curDir = "/";
  72. for (int i = 0; i < dirs.Length; i++)
  73. {
  74. string dir = dirs[i];
  75. //如果是以/开始的路径,第一个为空
  76. if (dir != null && dir.Length > 0)
  77. {
  78. try
  79. {
  80. curDir += dir + "/";
  81. FtpMakeDir(curDir);
  82. }
  83. catch (Exception ex)
  84. {
  85. }
  86. }
  87. }
  88. return "";
  89. }
  90. public string FtpParseDirectory(string destFilePath)
  91. {
  92. return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
  93. }
  94. public Boolean FtpMakeDir(string localFile)
  95. {
  96. //Console.WriteLine(ftpServerIP + localFile);
  97. FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpServerIP + localFile);
  98. try
  99. {
  100. req.Credentials = new NetworkCredential(ftpUser, ftpPwd);
  101. req.Method = WebRequestMethods.Ftp.MakeDirectory;
  102. FtpWebResponse response = (FtpWebResponse)req.GetResponse();
  103. response.Close();
  104. }
  105. catch (Exception ex)
  106. {
  107. Console.WriteLine(ex.Message);
  108. req.Abort();
  109. return false;
  110. }
  111. req.Abort();
  112. return true;
  113. }
  114. }
  115. }