C#文件(夹)在ftp服务器间的同步
Posted yuzhihui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于C#文件(夹)在ftp服务器间的同步的主要内容,如果未能解决你的问题,请参考以下文章