C # file (folder) synchronization between ftp servers,
1 # region move file (folder) 1 from ftp Server 1 to ftp Server 2) 2 2 // <summary> 3 // move folder 1 from ftp Server 1 to ftp Server 2 folder 2 4 /// </summary> 5 // <param name = "sFtpUriD"> source ftp url: ftp://ip+port </Param> 6 // <param name = "sFilePathD"> source ftp file path: /xxx </param> 7 // <param name = "sFtpUserD"> source ftp Username </param> 8 // <param name = "sFtpPwdD"> source ftp password </param> 9 // <param name = "sFtpUriU"> target ftp url: ftp://ip+port </Param> 10 // <param name = "sFilePathU"> target ftp file path: /xxx </param> 11 // <param name = "sFtpUserU"> target ftp user name </param> 12 // <param name = "sFtpPwdU"> target ftp password </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 // obtain detailed file list information in the folder (including file size information) 18 string [] sFileNameArr = GetFileList (sFtpUriD + sFolderPathD, WebRequestMethods. ftp. listDirectoryDetails, sFtpUserD, sFtpPwdD); 19 // determine whether a single file is 20 if (sFileNameArr. length <= 2) // The folder contains a single file 21 {22 // create a folder on ftp Server 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 bytes (sFtpUriD, sFolderPathD + "/" + onlynames, sFtpUserD, fingerprint, 37 sFtpUriU, sFolderPathU + "/" + onlynames, sFtpUserU, sFtpPwdU ); 38 break; 39} 40} 41} 42} 43 else // The folder contains multiple files or subfolders 44 {45 // create a folder 46 FtpCheckDirectoryExist (sFtpUriU + "/" on ftp Server 2 "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU); 47 48 foreach (string sFileName in sFileNameArr) 49 {50 // determine whether a folder id <DIR> 51 if (sFileName. contains ("<DIR>") 52 {53 string sInnerFloder = sFileName. split (new string [] {"<DIR>"}, StringSplitOptions. none) [1]. trim (); 54 55 Gbit/s (sFtpUriD, sFolderPathD + "/" + sInnerFloder, sFtpUserD, sFtpPwdD, 56 sFtpUriU, sFolderPathU + "/" + sInnerFloder, sFtpUserU ); // recursive folder 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 records (sFtpUriD, sFolderPathD +"/"+ onlynames, sFtpUserD, records, 72 sFtpUriU, sFolderPathU +"/"+ onlynames, sFtpUserU, sFtpPwdU); 73 break; 74} 75} 76} 77} 78} 79} 80 81 return true; 82} 83 // <summary> 84 // move the file from ftp Server 1 to ftp Server 2 file 85 /// </summary> 86 // <param name = "sFtpUriD"> source ftp url: ftp://ip+port </Param> 87 // <param name = "sFilePathD"> source ftp file path: /xxx </param> 88 // <param name = "sFtpUserD"> source ftp user name </param> 89 // <param name = "sFtpPwdD"> source ftp password </param> 90 // <param name = "sFtpUriU"> target ftp url: ftp://ip+port </Param> 91 // <param name = "sFilePathU"> target ftp file path: /xxx </param> 92 // <param name = "sFtpUserU"> target ftp user name </param> 93 // <param name = "sFtpPwdU"> target ftp password </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 // In the target ftp Create a 101 string sFolderPathU = sFilePathU folder on the server. substring (0, sFilePathU. lastIndexOf ("/"); 102 FtpCheckDirectoryExist (sFtpUriU + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU); 103 104/1. download from source ftp Server 1 (oral) 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; // download method 110 reqFtpDownload. keepAlive = false; 111 reqFtpDownload. useBinary = true; 112 reqFtpDownload. proxy = null; 113 114 FtpWebResponse response = (FtpWebResponse) reqFtpDownload. getResponse (); 115 116 // directly upload the response Stream downloaded from Server 1 as the upload stream to server 2 117 Stream stream = response. getResponseStream (); 118 119 // 2. upload to the target ftp Server 2 (Question Bank) 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; // upload method 125 reqFTPUpload. keepAlive = false; 126 reqFTPUpload. useBinary = true; 127 reqFTPUpload. proxy = null; 128 129 int buffLength = 2048; // 2 kb130 byte [] buff = new byte [BuffLength]; 131 132 Stream requestStream = reqFTPUpload. getRequestStream (); 133 int len = stream. read (buff, 0, buff. length); // the file size is 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 (); // release resource 144 requestStream. dispose (); // release resource 145 146 string sErr = "file sync successful:" + sFtpUriD + SFilePathD; 147 WriteError (sErr); 148 149 return true; 150} 151 catch (Exception ex) 152 {153 ex. toString (); 154 string sErr = "file sync failed:" + sFtpUriD + sFilePathD; 155 WriteError (sErr); 156 return false; 157} 158} 159 // <summary> 160 // obtain the file (folder) in the folder on the ftp) list information 161 // </summary> 162 // <param name = "sFloderPath"> ftp path </param> 163 // <param name = "type"> type of the list to be retrieved (detailed or only file names) </param> 164 // <param name = "sFtpUser"> ftp Username </param> 165 // <param name = "sFtpPwd"> ftp password </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 try175 {176 ftpRequest = (FtpWebRequest) WebRequest. crea Te (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