C# 实现文件(夹)在ftp服务器间的同步【无需将文件(夹)保存到本地】
C#实现不同ftp服务器间文件(夹)同步
图1 实现不同ftp服务器间文件(夹)同步的实现思路图
/// <summary> /// 将文件夹1从ftp服务器1移到ftp服务器2文件夹2 /// </summary> /// <param name="sFtpUriD">源ftp url:ftp://ip+port</param> /// <param name="sFilePathD">源ftp 文件路径:/xxx/xxx/xxx</param> /// <param name="sFtpUserD">源ftp用户名</param> /// <param name="sFtpPwdD">源ftp密码</param> /// <param name="sFtpUriU">目的ftp url:ftp://ip+port</param> /// <param name="sFilePathU">目的ftp 文件路径:/xxx/xxx/xxx</param> /// <param name="sFtpUserU">目的ftp用户名</param> /// <param name="sFtpPwdU">目的ftp密码</param> /// <returns></returns> public bool MoveFolderOnDiffFtp(string sFtpUriD, string sFolderPathD, string sFtpUserD, string sFtpPwdD, string sFtpUriU, string sFolderPathU, string sFtpUserU, string sFtpPwdU) { //获取文件夹中的详细文件列表信息(包括文件大小信息) string[] sFileNameArr = GetFileList(sFtpUriD + sFolderPathD, WebRequestMethods.Ftp.ListDirectoryDetails, sFtpUserD, sFtpPwdD); //判断是否为单个文件 if (sFileNameArr.Length <= 2) //文件夹中存在单文件 { //在ftp服务器2上创建文件夹 FtpCheckDirectoryExist(sFtpUriU + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU); if (sFileNameArr[sFileNameArr.Length - 1] == "") { string[] onlyname = GetFileList(sFtpUriD + sFolderPathD, WebRequestMethods.Ftp.ListDirectory, sFtpUserD, sFtpPwdD); foreach (string onlynames in onlyname) { if (onlynames == "" || onlynames == " ") { break; } else { MoveFileOnDiffFtp(sFtpUriD, sFolderPathD + "/" + onlynames, sFtpUserD, sFtpPwdD, sFtpUriU, sFolderPathU + "/" + onlynames, sFtpUserU, sFtpPwdU); break; } } } } else //文件夹中存在多文件或子文件夹 { //在ftp服务器2上创建文件夹 FtpCheckDirectoryExist(sFtpUriU + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU); foreach (string sFileName in sFileNameArr) { //判断是否具有文件夹标识<DIR> if (sFileName.Contains("<DIR>")) { string sInnerFloder = sFileName.Split(new string[] { "<DIR>" }, StringSplitOptions.None)[1].Trim(); MoveFolderOnDiffFtp(sFtpUriD, sFolderPathD + "/" + sInnerFloder, sFtpUserD, sFtpPwdD, sFtpUriU, sFolderPathU + "/" + sInnerFloder, sFtpUserU, sFtpPwdU); //递归文件夹中的子文件夹 } else { string[] onlyname = GetFileList(sFtpUriD + sFolderPathD, WebRequestMethods.Ftp.ListDirectory, sFtpUserD, sFtpPwdD); foreach (string onlynames in onlyname) { if (onlynames == "" || onlynames == " " || sFileName == "") { break; } else { if (sFileName.Contains(" " + onlynames)) { MoveFileOnDiffFtp(sFtpUriD, sFolderPathD + "/" + onlynames, sFtpUserD, sFtpPwdD, sFtpUriU, sFolderPathU + "/" + onlynames, sFtpUserU, sFtpPwdU); break; } } } } } } return true; } /// <summary> /// 将文件从ftp服务器1移到ftp服务器2文件 /// </summary> /// <param name="sFtpUriD">源ftp url:ftp://ip+port</param> /// <param name="sFilePathD">源ftp 文件路径:/xxx/xxx/xxx</param> /// <param name="sFtpUserD">源ftp用户名</param> /// <param name="sFtpPwdD">源ftp密码</param> /// <param name="sFtpUriU">目的ftp url:ftp://ip+port</param> /// <param name="sFilePathU">目的ftp 文件路径:/xxx/xxx/xxx</param> /// <param name="sFtpUserU">目的ftp用户名</param> /// <param name="sFtpPwdU">目的ftp密码</param> /// <returns></returns> public bool MoveFileOnDiffFtp(string sFtpUriD, string sFilePathD, string sFtpUserD, string sFtpPwdD, string sFtpUriU, string sFilePathU, string sFtpUserU, string sFtpPwdU) { try { //在目的ftp服务器上创建文件夹 string sFolderPathU = sFilePathU.Substring(0, sFilePathU.LastIndexOf("/")); FtpCheckDirectoryExist(sFtpUriU + "/", sFolderPathU + "/", sFtpUserU, sFtpPwdU); //1.从源ftp服务器1下载 FtpWebRequest reqFtpDownload; reqFtpDownload = (FtpWebRequest)FtpWebRequest.Create(new Uri(sFtpUriD + sFilePathD)); reqFtpDownload.Credentials = new NetworkCredential(sFtpUserD, sFtpPwdD); reqFtpDownload.Method = WebRequestMethods.Ftp.DownloadFile; //下载方法 reqFtpDownload.KeepAlive = false; reqFtpDownload.UseBinary = true; reqFtpDownload.Proxy = null; FtpWebResponse response = (FtpWebResponse)reqFtpDownload.GetResponse(); //将从服务器1下载的响应流直接作为上传到服务器2的上传流 Stream stream = response.GetResponseStream(); //2.上传到目的ftp服务器2 FtpWebRequest reqFTPUpload; reqFTPUpload = (FtpWebRequest)FtpWebRequest.Create(new Uri(sFtpUriU + sFilePathU)); reqFTPUpload.Credentials = new NetworkCredential(sFtpUserU, sFtpPwdU); reqFTPUpload.Method = WebRequestMethods.Ftp.UploadFile; //上传方法 reqFTPUpload.KeepAlive = false; reqFTPUpload.UseBinary = true; reqFTPUpload.Proxy = null; int buffLength = 2048; //每次读入文件流2kb byte[] buff = new byte[buffLength]; Stream requestStream = reqFTPUpload.GetRequestStream(); int len = stream.Read(buff, 0, buff.Length); //文件大小 while (len > 0) { requestStream.Write(buff, 0, len); len = stream.Read(buff, 0, buffLength); } stream.Close(); requestStream.Close(); stream.Dispose();//释放资源 requestStream.Dispose();//释放资源return true; } catch (Exception ex) { return false; } } /// <summary> /// 获取ftp上文件夹中的文件(夹)列表信息 /// </summary> /// <param name="sFloderPath">ftp路径</param> /// <param name="type">获取列表的类型(详细或只有文件名)</param> /// <param name="sFtpUser">ftp用户名</param> /// <param name="sFtpPwd">ftp密码</param> /// <returns></returns> private string[] GetFileList(string sFloderPath, string type ,string sFtpUser,string sFtpPwd) { WebResponse webresp = null; StreamReader ftpFileListReader = null; FtpWebRequest ftpRequest = null; StringBuilder str = new StringBuilder(); try { ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(sFloderPath)); ftpRequest.Credentials = new NetworkCredential(sFtpUser, sFtpPwd); ftpRequest.Method = type; webresp = ftpRequest.GetResponse(); ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.UTF8); string line = ftpFileListReader.ReadLine(); while (line != null) { str.Append(line); str.Append("/n"); line = ftpFileListReader.ReadLine(); } } catch (Exception ex) {} return str.ToString().Split(new string[] { "/n" }, StringSplitOptions.None); } /// <summary> /// 判断文件的目录是否存,不存则创建 /// </summary> /// <param name="destFilePath"></param> private static void FtpCheckDirectoryExist(string strFtpURI, string destFilePath, string strFtpUserID, string strFtpPassword) { string fullDir = destFilePath.Substring(0, destFilePath.LastIndexOf("/")); string[] dirs = fullDir.Split('/'); string curDir = ""; for (int i = 0; i < dirs.Length; i++) { string dir = dirs[i]; //如果是以/开始的路径,第一个为空 if (dir != null && dir.Length > 0) { try { curDir += dir + "/"; FtpMakeDir(strFtpURI, curDir, strFtpUserID, strFtpPassword); } catch (Exception) {} } } } /// <summary> /// 创建ftp目录 /// </summary> private static Boolean FtpMakeDir(string strFtpURI, string destFilePath, string strFtpUserID, string strFtpPassword) { FtpWebRequest req = (FtpWebRequest)WebRequest.Create(strFtpURI + destFilePath); req.Credentials = new NetworkCredential(strFtpUserID, strFtpPassword); req.Proxy = null; req.KeepAlive = false; req.Method = WebRequestMethods.Ftp.MakeDirectory;//请求方法为创建目录方法 req.UseBinary = true; FtpWebResponse response = req.GetResponse() as FtpWebResponse; Stream ftpStream = response.GetResponseStream(); ftpStream.Close(); response.Close(); req.Abort(); return true; }
......