程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#文件(夾)在ftp服務器間的同步,

C#文件(夾)在ftp服務器間的同步,

編輯:C#入門知識

C#文件(夾)在ftp服務器間的同步,


  1 #region 將文件(夾)1從ftp服務器1移到ftp服務器2文件(夾)2
  2         /// <summary>
  3         /// 將文件夾1從ftp服務器1移到ftp服務器2文件夾2
  4         /// </summary>
  5         /// <param name="sFtpUriD">源ftp url:ftp://ip+port</param>
  6         /// <param name="sFilePathD">源ftp 文件路徑:/xxx/xxx/xxx</param>
  7         /// <param name="sFtpUserD">源ftp用戶名</param>
  8         /// <param name="sFtpPwdD">源ftp密碼</param>
  9         /// <param name="sFtpUriU">目的ftp url:ftp://ip+port</param>
 10         /// <param name="sFilePathU">目的ftp 文件路徑:/xxx/xxx/xxx</param>
 11         /// <param name="sFtpUserU">目的ftp用戶名</param>
 12         /// <param name="sFtpPwdU">目的ftp密碼</param>
 13         /// <returns></returns>
 14         public bool MoveFolderOnDiffFtp(string sFtpUriD, string sFolderPathD, string sFtpUserD, string sFtpPwdD,
 15             string sFtpUriU, string sFolderPathU, string sFtpUserU, string sFtpPwdU)
 16         {
 17             //獲取文件夾中的詳細文件列表信息(包括文件大小信息)
 18             string[] sFileNameArr = GetFileList(sFtpUriD + sFolderPathD, WebRequestMethods.Ftp.ListDirectoryDetails, sFtpUserD, sFtpPwdD);
 19             //判斷是否為單個文件
 20             if (sFileNameArr.Length <= 2)       //文件夾中存在單文件
 21             {
 22                 //在ftp服務器2上創建文件夾
 23                 FtpCheckDirectoryExist(sFtpUriU + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU);
 24 
 25                 if (sFileNameArr[sFileNameArr.Length - 1] == "")
 26                 {
 27                     string[] onlyname = GetFileList(sFtpUriD + sFolderPathD, WebRequestMethods.Ftp.ListDirectory, sFtpUserD, sFtpPwdD);
 28                     foreach (string onlynames in onlyname)
 29                     {
 30                         if (onlynames == "" || onlynames == " ")
 31                         {
 32                             break;
 33                         }
 34                         else
 35                         {
 36                             MoveFileOnDiffFtp(sFtpUriD, sFolderPathD + "/" + onlynames, sFtpUserD, sFtpPwdD,
 37                                 sFtpUriU, sFolderPathU + "/" + onlynames, sFtpUserU, sFtpPwdU);
 38                             break;
 39                         }
 40                     }
 41                 }
 42             }
 43             else        //文件夾中存在多文件或子文件夾
 44             {
 45                 //在ftp服務器2上創建文件夾
 46                 FtpCheckDirectoryExist(sFtpUriU + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU);
 47 
 48                 foreach (string sFileName in sFileNameArr)
 49                 {
 50                     //判斷是否具有文件夾標識<DIR>
 51                     if (sFileName.Contains("<DIR>"))
 52                     {
 53                         string sInnerFloder = sFileName.Split(new string[] { "<DIR>" }, StringSplitOptions.None)[1].Trim();
 54 
 55                         MoveFolderOnDiffFtp(sFtpUriD, sFolderPathD + "/" + sInnerFloder, sFtpUserD, sFtpPwdD,
 56                             sFtpUriU, sFolderPathU + "/" + sInnerFloder, sFtpUserU, sFtpPwdU);    //遞歸文件夾中的子文件夾
 57                     }
 58                     else
 59                     {
 60                         string[] onlyname = GetFileList(sFtpUriD + sFolderPathD, WebRequestMethods.Ftp.ListDirectory, sFtpUserD, sFtpPwdD);
 61                         foreach (string onlynames in onlyname)
 62                         {
 63                             if (onlynames == "" || onlynames == " " || sFileName == "")
 64                             {
 65                                 break;
 66                             }
 67                             else
 68                             {
 69                                 if (sFileName.Contains(" " + onlynames))
 70                                 {                                    
 71                                     MoveFileOnDiffFtp(sFtpUriD, sFolderPathD + "/" + onlynames, sFtpUserD, sFtpPwdD,
 72                                         sFtpUriU, sFolderPathU + "/" + onlynames, sFtpUserU, sFtpPwdU);
 73                                     break;
 74                                 }
 75                             }
 76                         }
 77                     }
 78                 }
 79             }
 80 
 81             return true;
 82         }
 83         /// <summary>
 84         /// 將文件從ftp服務器1移到ftp服務器2文件
 85         /// </summary>
 86         /// <param name="sFtpUriD">源ftp url:ftp://ip+port</param>
 87         /// <param name="sFilePathD">源ftp 文件路徑:/xxx/xxx/xxx</param>
 88         /// <param name="sFtpUserD">源ftp用戶名</param>
 89         /// <param name="sFtpPwdD">源ftp密碼</param>
 90         /// <param name="sFtpUriU">目的ftp url:ftp://ip+port</param>
 91         /// <param name="sFilePathU">目的ftp 文件路徑:/xxx/xxx/xxx</param>
 92         /// <param name="sFtpUserU">目的ftp用戶名</param>
 93         /// <param name="sFtpPwdU">目的ftp密碼</param>
 94         /// <returns></returns>
 95         public bool MoveFileOnDiffFtp(string sFtpUriD, string sFilePathD, string sFtpUserD, string sFtpPwdD,
 96             string sFtpUriU, string sFilePathU, string sFtpUserU, string sFtpPwdU)
 97         {
 98             try
 99             {                
100                 //在目的ftp服務器上創建文件夾
101                 string sFolderPathU = sFilePathU.Substring(0, sFilePathU.LastIndexOf("/")); 
102                 FtpCheckDirectoryExist(sFtpUriU + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU);
103 
104                 //1.從源ftp服務器1下載(口語)
105                 FtpWebRequest reqFtpDownload;
106 
107                 reqFtpDownload = (FtpWebRequest)FtpWebRequest.Create(new Uri(sFtpUriD + sFilePathD));
108                 reqFtpDownload.Credentials = new NetworkCredential(sFtpUserD, sFtpPwdD);
109                 reqFtpDownload.Method = WebRequestMethods.Ftp.DownloadFile; //下載方法
110                 reqFtpDownload.KeepAlive = false;
111                 reqFtpDownload.UseBinary = true;
112                 reqFtpDownload.Proxy = null;
113 
114                 FtpWebResponse response = (FtpWebResponse)reqFtpDownload.GetResponse();
115 
116                 //將從服務器1下載的響應流直接作為上傳到服務器2的上傳流
117                 Stream stream = response.GetResponseStream();
118 
119                 //2.上傳到目的ftp服務器2(題庫)
120                 FtpWebRequest reqFTPUpload;
121 
122                 reqFTPUpload = (FtpWebRequest)FtpWebRequest.Create(new Uri(sFtpUriU + sFilePathU));
123                 reqFTPUpload.Credentials = new NetworkCredential(sFtpUserU, sFtpPwdU);
124                 reqFTPUpload.Method = WebRequestMethods.Ftp.UploadFile; //上傳方法
125                 reqFTPUpload.KeepAlive = false;
126                 reqFTPUpload.UseBinary = true;
127                 reqFTPUpload.Proxy = null;
128 
129                 int buffLength = 2048;  //每次讀入文件流2kb
130                 byte[] buff = new byte[buffLength];
131 
132                 Stream requestStream = reqFTPUpload.GetRequestStream();
133                 int len = stream.Read(buff, 0, buff.Length);  //文件大小
134 
135                 while (len > 0)
136                 {
137                     requestStream.Write(buff, 0, len);
138                     len = stream.Read(buff, 0, buffLength);
139                 }
140 
141                 stream.Close();
142                 requestStream.Close();
143                 stream.Dispose();//釋放資源
144                 requestStream.Dispose();//釋放資源
145                 
146                 string sErr = "文件同步成功:" + sFtpUriD + sFilePathD;
147                 WriteError(sErr);
148 
149                 return true;
150             }
151             catch (Exception ex)
152             {
153                 ex.ToString();
154                 string sErr = "文件同步失敗:" + sFtpUriD + sFilePathD;
155                 WriteError(sErr);
156                 return false;
157             }
158         }
159         /// <summary>
160         /// 獲取ftp上文件夾中的文件(夾)列表信息
161         /// </summary>
162         /// <param name="sFloderPath">ftp路徑</param>
163         /// <param name="type">獲取列表的類型(詳細或只有文件名)</param>
164         /// <param name="sFtpUser">ftp用戶名</param>
165         /// <param name="sFtpPwd">ftp密碼</param>
166         /// <returns></returns>
167         private string[] GetFileList(string sFloderPath, string type ,string sFtpUser,string sFtpPwd)
168         {
169             WebResponse webresp = null;
170             StreamReader ftpFileListReader = null;
171             FtpWebRequest ftpRequest = null;
172             StringBuilder str = new StringBuilder();            
173 
174             try
175             {
176                 ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(sFloderPath));
177                 ftpRequest.Credentials = new NetworkCredential(sFtpUser, sFtpPwd);
178                 ftpRequest.Method = type;
179                 webresp = ftpRequest.GetResponse();
180                 ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.UTF8);
181                 
182                 string line = ftpFileListReader.ReadLine();
183                 while (line != null)
184                 {
185                     str.Append(line);
186                     str.Append("/n");
187                     line = ftpFileListReader.ReadLine();
188                 }
189             }
190             catch (Exception ex)
191             {
192                 WriteError(ex.ToString());
193             }
194 
195             return str.ToString().Split(new string[] { "/n" }, StringSplitOptions.None);
196         }
197 
198         #endregion

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved