private void DownloadFile() { log.Info("Start download file from ftp according to FtpControl table."); IList<DssFtpControl> dssFtpControlList = this.dssFtpControlMgr.GetDssFtpControl(BusinessConstants.IO_TYPE_IN); if (dssFtpControlList != null && dssFtpControlList.Count > 0) { foreach (DssFtpControl dssFtpControl in dssFtpControlList) { string ftpServer = string.Empty; int ftpPort = 21; string ftpInboundFolder = string.Empty; string ftpUser = string.Empty; string ftpPass = string.Empty; string filePattern = string.Empty; string localTempFolder = string.Empty; string localFolder = string.Empty; string ftpBackupFolder = string.Empty; try { #region 获取ftp参数 ftpServer = dssFtpControl.FtpServer; ftpPort = dssFtpControl.FtpPort.HasValue ? dssFtpControl.FtpPort.Value : 21; ftpInboundFolder = dssFtpControl.FtpFolder; ftpBackupFolder = dssFtpControl.FtpTempFolder; ftpUser = dssFtpControl.FtpUser; ftpPass = dssFtpControl.FtpPassword; filePattern = dssFtpControl.FilePattern; #endregion #region 初始化本地目录 localTempFolder = dssFtpControl.LocalTempFolder; localTempFolder = localTempFolder.Replace("\\", "/"); if (!localTempFolder.EndsWith("/")) { localTempFolder += "/"; } if (!Directory.Exists(localTempFolder)) { Directory.CreateDirectory(localTempFolder); } localFolder = dssFtpControl.LocalFolder; localFolder = localFolder.Replace("\\", "/"); if (!localFolder.EndsWith("/")) { localFolder += "/"; } if (!Directory.Exists(localFolder)) { Directory.CreateDirectory(localFolder); } #endregion #region 下载文件 FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpInboundFolder, ftpUser, ftpPass); foreach (string fileName in ftp.GetFileList(filePattern)) { try { ftp.Download(localTempFolder, fileName); log.Info("Move file from folder: " + localTempFolder + fileName + " to folder: " + localFolder + fileName); File.Move(localTempFolder + fileName, localFolder + fileName); if (ftpBackupFolder != null && ftpBackupFolder.Length > 0) { ftp.MovieFile(fileName, ftpBackupFolder); } else { ftp.Delete(fileName); } } catch (Exception ex) { log.Error("Download file:" + fileName, ex); } } #endregion } catch (Exception ex) { log.Error("Download files from ftpServer:" + ftpServer, ex); } } } else { log.Info("No record found in FtpControl table."); } log.Info("End download file from ftp according to FtpControl table."); }
private void UploadFile() { log.Info("Start upload file to ftp according to FtpControl table."); IList<DssFtpControl> dssFtpControlList = this.dssFtpControlMgrE.GetDssFtpControl(BusinessConstants.IO_TYPE_OUT); if (dssFtpControlList != null && dssFtpControlList.Count > 0) { foreach (DssFtpControl dssFtpControl in dssFtpControlList) { string ftpServer = string.Empty; int ftpPort = 21; string ftpTempFolder = string.Empty; string ftpFolder = string.Empty; string ftpUser = string.Empty; string ftpPass = string.Empty; string filePattern = string.Empty; string localFolder = string.Empty; try { #region 获取参数 ftpServer = dssFtpControl.FtpServer; ftpPort = dssFtpControl.FtpPort.HasValue ? dssFtpControl.FtpPort.Value : 21; ftpTempFolder = dssFtpControl.FtpTempFolder; ftpFolder = dssFtpControl.FtpFolder; ftpUser = dssFtpControl.FtpUser; ftpPass = dssFtpControl.FtpPassword; filePattern = dssFtpControl.FilePattern; localFolder = dssFtpControl.LocalFolder; #endregion #region 初始化远程目录 FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpTempFolder, ftpUser, ftpPass); ftpTempFolder = ftpTempFolder.Replace("\\", "/"); if (!ftpTempFolder.EndsWith("/")) { ftpTempFolder += "/"; } try { //清空Temp目录 foreach (string fileName in ftp.GetFileList(filePattern)) { ftp.Delete(fileName); } } catch (Exception) { } //if (!ftp.DirectoryExist(ftpTempFolder)) //{ // ftp.MakeDir(ftpTempFolder); //} ftpFolder = ftpFolder.Replace("\\", "/"); if (!ftpFolder.EndsWith("/")) { ftpFolder += "/"; } //if (!ftp.DirectoryExist(ftpFolder)) //{ // ftp.MakeDir(ftpFolder); //} #endregion #region 获取本地上传文件列表 string[] files = null; if (filePattern != null) { files = Directory.GetFiles(localFolder, filePattern); } else { files = Directory.GetFiles(localFolder); } #endregion #region 上传文件 if (files != null && files.Length > 0) { foreach (string fileFullPath in files) { try { string fomatedFileFullPath = fileFullPath.Replace("\\", "/"); string fileName = fomatedFileFullPath.Substring(fomatedFileFullPath.LastIndexOf("/") + 1); ftp.Upload(fomatedFileFullPath); ftp.MovieFile(fileName, ftpFolder + fileName); log.Info("Delete file: " + fomatedFileFullPath); File.Delete(fomatedFileFullPath); } catch (Exception ex) { log.Error("Upload file:" + fileFullPath, ex); } } } #endregion } catch (Exception ex) { log.Error("Upload files from ftpServer:" + ftpServer, ex); } } } else { log.Info("No record found in FtpControl table."); } log.Info("End upload file to ftp according to FtpControl table."); }