/// <summary> /// 下载FTP文件 /// </summary> /// <param name="ftpFilePath">FTP文件路径</param> /// <param name="localFileName">下载的文件名</param> /// <param name="ftpConfigName">FTP配置节点名</param> public void DownloadFtpFile(string ftpFilePath, string localFileName = null, string ftpConfigName = null) { FtpClient ftp = FtpConfigManager.GetFtpClient(ftpConfigName); string localFile = Path.Combine(LocalFilePath, Path.GetFileName(ftpFilePath)); try { ftp.Connect(); if (ftp.IsConnected) { ftp.OpenDownload(ftpFilePath, localFile); while (ftp.DoDownload() > 0) { //ftp.BytesTotal //已经下载的字节 } } } catch (Exception ex) { throw; }finally{ //关闭ftp连接 ftp.Disconnect(); } if (!File.Exists(localFile)) { throw new FileNotFoundException("FTP文件下载到本地失败,本地文件目录:" + localFile); } DownloadServerFile(localFile, fileName); //删除本地文件 File.Delete(localFile); }
public Response <FtpConfig> GetFtpConfigByName(TokenRequest <GetFtpConfigByNameRequest> request) { Response <FtpConfig> response = new Response <FtpConfig>(); try { if (request == null || request.RequestData == null) { response.Code = -1; response.Description = "请求参数为空!"; response.ResponseData = null; } else { FtpConfigManager manager = new FtpConfigManager(request.Version); ManagerResult <FtpConfig> result = manager.GetFtpConfigByName(request.RequestData.Name); response.GetResultInfo(result); response.ResponseData = result.ResultData; } } catch (Exception ex) { LogHelper.WriteLog(LogType.Error, ex); response.Code = -1; } return(response); }
/// <summary> /// 保存文件到FTP指定目录上 /// </summary> /// <param name="savePath">保存的FTP路径</param> /// <param name="serverConfigName">FTP配置名</param> /// <returns>返回上传之后的文件路径</returns> public string UploadFtp(string savePath, string serverConfigName = null) { //读取ftp配置 FtpClient ftp = FtpConfigManager.GetFtpClient(serverConfigName); string uploadFile = null; try { ftp.Connect(); if (ftp.IsConnected) { //判断ftp文件夹是否存在,不存在新建 ftp.MakeDir(savePath); ftp.ChangeDir(savePath); ftp.OpenUpload(this.file.InputStream, NewFileName); while (ftp.DoUpload() > 0) { //ftp.BytesTotal 已上传的字节 } uploadFile = UriHelper.Combine(savePath, NewFileName); } } catch { throw; } finally { ftp.Disconnect(); } return(uploadFile); }