/// <summary> /// 在Ftp服务器上创建文件夹. /// </summary> /// <param name="iUrl">URL.</param> /// <param name="iAccountId">账户.</param> /// <param name="iPwd">密码.</param> private TDirState CreateDirOnFtpServer( string iUrl, string iAccountId, string iPwd) { FtpWebRequest ftpRequest = null; FtpWebResponse response = null; TDirState state = TDirState.None; try { Uri targetURI = new Uri(iUrl); ftpRequest = (FtpWebRequest)FtpWebRequest.Create(targetURI); ftpRequest.Credentials = new NetworkCredential(iAccountId, iPwd); ftpRequest.KeepAlive = false; ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory; ftpRequest.UseBinary = true; response = ftpRequest.GetResponse() as FtpWebResponse; state = TDirState.Created; UtilsLog.Info("CreateDirOnFtpServer", "Successed Url:{0}", iUrl); } catch (WebException exp) { UtilsLog.Exception("CreateDirOnFtpServer", "Failed!!! Url:{0} \n WebException: \n {1}", iUrl, exp.Message); state = TDirState.Exception; } catch (IOException exp) { UtilsLog.Exception("CreateDirOnFtpServer", "Failed!!! Url:{0} \n IOException: \n {1}", iUrl, exp.Message); state = TDirState.Exception; } catch (Exception exp) { UtilsLog.Exception("CreateDirOnFtpServer", "Failed!!! Url:{0} \n Exception: \n {1}", iUrl, exp.Message); state = TDirState.Exception; } finally { if (response != null) { response.Close(); } } return(state); }
/// <summary> /// 检测服务器上Dir信息. /// </summary> /// <returns><c>true</c>, 成功, <c>false</c> 失败.</returns> /// <param name="iServer">服务器信息.</param> /// <param name="iParentUrl">上一层Url.</param> /// <param name="iCheckDir">检测.</param> private bool CheckDirOnServer(UploadServerInfo iServer, string iParentUrl, string iCheckDir) { if (string.IsNullOrEmpty(iParentUrl) == true) { return(false); } if (string.IsNullOrEmpty(iCheckDir) == true) { return(false); } TDirState state = TDirState.None; // FTP if (TUploadWay.Ftp == this._uploadWay) { state = this.GetDirStateOnFtpServer( string.Format("{0}/", iParentUrl), iCheckDir, iServer.AccountId, iServer.Pwd); } switch (state) { case TDirState.Exist: { return(true); } break; case TDirState.NotExist: { string createUrl = string.Format("{0}/{1}", iParentUrl, iCheckDir); if (TUploadWay.Ftp == this._uploadWay) { state = this.CreateDirOnFtpServer(createUrl, iServer.AccountId, iServer.Pwd); if (TDirState.Created == state) { return(true); } } } break; default: break; } return(false); }
/// <summary> /// 取得指定目录在Ftp服务器上得状态. /// </summary> /// <returns>目录在服务器上得状态.</returns> /// <param name="iParentUrl">上一层URL.</param> /// <param name="iTargetDir">目标目录.</param> /// <param name="iAccountId">账户.</param> /// <param name="iPwd">密码.</param> private TDirState GetDirStateOnFtpServer( string iParentUrl, string iTargetDir, string iAccountId, string iPwd) { FtpWebRequest ftpRequest = null; FtpWebResponse response = null; StreamReader reader = null; TDirState state = TDirState.None; try { Uri targetURI = new Uri(iParentUrl); ftpRequest = (FtpWebRequest)FtpWebRequest.Create(targetURI); ftpRequest.Credentials = new NetworkCredential(iAccountId, iPwd); ftpRequest.KeepAlive = false; ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; ftpRequest.UseBinary = true; response = ftpRequest.GetResponse() as FtpWebResponse; reader = new StreamReader( response.GetResponseStream(), System.Text.Encoding.Default); string line = reader.ReadLine(); state = TDirState.NotExist; // 循环遍历内容 while (line != null) { if (line.Equals(iTargetDir) == true) { state = TDirState.Exist; break; } line = reader.ReadLine(); } if (TDirState.NotExist == state) { UtilsLog.Info("GetDirStateOnFtpServer", "State:{0} ParentUrl:{1} TargetDir:{2}", state.ToString(), iParentUrl, iTargetDir); } } catch (WebException exp) { UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n WebException: \n {2}", iParentUrl, iTargetDir, exp.Message); state = TDirState.Exception; } catch (IOException exp) { UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n IOException: \n {2}", iParentUrl, iTargetDir, exp.Message); state = TDirState.Exception; } catch (Exception exp) { UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n Exception: \n {2}", iParentUrl, iTargetDir, exp.Message); state = TDirState.Exception; } finally { if (reader != null) { reader.Close(); } if (response != null) { response.Close(); } } return(state); }