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 = "s FilePathU "> 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 [] sFil ENameArr = 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 + sFolderPath D, 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 + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU) on ftp Server 2; 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 MoveFolderOnDiffFtp (sFtpUriD, sFolderPathD + "/" + sInnerFloder, sFtpUser D, sFtpPwdD, 56 sFtpUriU, sFolderPathU + "/" + sInnerFloder, sFtpUserU, sFtpPwdU ); // 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. contai Ns ("+ onlynames) 70 {71 records (sFtpUriD, sFolderPathD +"/"+ onlynames, sFtpUserD, fingerprint, 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 MoveFileOnDiffFt P (string sFtpUriD, string sFilePathD, string sFtpUserD, string sFtpPwdD, 96 string sFtpUriU, string sFilePathU, string sFtpUserU, string sFtpPwdU) 97 {98 try 99 {100 // create the folder 101 string sFolderPathU = sFilePathU on the target ftp server. substring (0, sFilePathU. lastIndexOf ("/"); 102 FtpCheckDirectoryExist (sFtpUriU + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU); 103 104/1. download from source ftp Server 1 (oral) 105 FtpWebRequest reqFtp Download; 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 = (FtpWebRespon Se) 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; // read the file Stream 2kb130 byte [] buff = new byte [buffLength] each time; 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 ftp folder) 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 user name </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 Stream Reader ftpFileListReader = null; 171 FtpWebRequest ftpRequest = null; 172 StringBuilder str = new StringBuilder (); 173 174 176 try175 {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. getResponseS Tream (), 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