/// <summary> /// 下载单文件 /// </summary> /// <param name="localDic">本地目录(@"D:\test")</param> /// <param name="downloadFileName">下载文件名称("abc.txt")</param> /// <param name="remotePath">远程路径("/test/abc.txt")</param> /// <returns></returns> public bool DownloadFile(string localDic, string downloadFileName, string remotePath) { Log4netUtil.Log4NetHelper.Info(String.Format(@"调用 DownloadFile->下载文件 参数 localDic:{0} ; downloadFileName: {1} ;remotePath: {2} ;", localDic, downloadFileName, remotePath), @"Ftp"); bool boolResult = false; string strFileName = string.Empty; string remotePathUrlEncode = remotePath; try { //本地目录不存在,则自动创建 if (!Util.FileHelper.IsValidPathChars(localDic)) { Log4netUtil.Log4NetHelper.Error(String.Format(@"DownloadFile->下载文件 异常:{0} 参数 localDic:{1} ; downloadFileName: {2} ;remotePath: {3} ;remotePathUrlEncode:{4}", "目录有非法字符", localDic, downloadFileName, remotePath, remotePathUrlEncode), @"Ftp"); return(boolResult); } if (!Directory.Exists(localDic)) { Directory.CreateDirectory(localDic); } //取下载文件的文件名 if (string.IsNullOrEmpty(downloadFileName)) { strFileName = Path.GetFileName(remotePath); } else { strFileName = downloadFileName; } //拼接本地路径 localDic = Path.Combine(localDic, strFileName); if (Connect()) { boolResult = ftpClient.DownloadFile(localDic, remotePathUrlEncode, FtpLocalExists.Overwrite); } if (boolResult) { Log4netUtil.Log4NetHelper.Info(String.Format(@"DownloadFile->下载文件成功! ;参数 localDic:{0} ; downloadFileName: {1} ;remotePath: {2} ;", localDic, downloadFileName, remotePath), @"Ftp"); } } catch (Exception ex) { Log4netUtil.Log4NetHelper.Error(String.Format(@"DownloadFile->下载文件 异常:{0} 参数 localDic:{1} ; downloadFileName: {2} ;remotePath: {3} ;remotePathUrlEncode:{4}", ex.ToString(), localDic, downloadFileName, remotePath, remotePathUrlEncode), @"Ftp"); } finally { DisConnect(); } return(boolResult); }
/// <summary> /// 下载文件(覆盖本地文件) /// </summary> /// <param name="localFile">下载到本地文件</param> /// <param name="file">服务器文件</param> public void DownloadFile(string localFile, string file) { if (ftp.FileExists(file)) { ftp.DownloadFile(localFile, file, FtpLocalExists.Overwrite, FtpVerify.None, this .ReturnProgress); } }