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."); }
public void DownloadFile() { //log.Info("Start download file from ftp according to FtpControl table."); IList<FtpControl> ftpControlList = this.genericMgr.FindAll<FtpControl>("from FtpControl fc where IOType=?", BusinessConstants.IO_TYPE_IN); //dssFtpControlMgr.GetDssFtpControl(BusinessConstants.IO_TYPE_IN); if (ftpControlList != null && ftpControlList.Count > 0) { foreach (FtpControl ftpControl in ftpControlList) { string ftpServer = string.Empty; int ftpPort = 21; string[] ftpInboundFolders; string ftpUser = string.Empty; string ftpPass = string.Empty; string filePattern = string.Empty; string localTempFolder = string.Empty; string localFolder = string.Empty; string ftpBackupFolder = string.Empty; string vaildFilePattern = string.Empty; try { #region 获取ftp参数 ftpServer = ftpControl.FtpServer; ftpPort = ftpControl.FtpPort.HasValue ? ftpControl.FtpPort.Value : 21; ftpInboundFolders = ftpControl.FtpFolder.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries); ftpBackupFolder = ftpControl.FtpTempFolder; ftpUser = ftpControl.FtpUser; ftpPass = ftpControl.FtpPassword; filePattern = ftpControl.FilePattern; vaildFilePattern = ftpControl.VaildFilePattern; #endregion #region 初始化本地目录 localTempFolder = ftpControl.LocalTempFolder; localTempFolder = localTempFolder.Replace("\\", "/"); if (!localTempFolder.EndsWith("/")) { localTempFolder += "/"; } if (!Directory.Exists(localTempFolder)) { Directory.CreateDirectory(localTempFolder); } localFolder = ftpControl.LocalFolder; localFolder = localFolder.Replace("\\", "/"); if (!localFolder.EndsWith("/")) { localFolder += "/"; } if (!Directory.Exists(localFolder)) { Directory.CreateDirectory(localFolder); } #endregion IList<InboundControl> inboundCtrlList = this.genericMgr.FindAll<InboundControl>(); #region 下载文件 foreach (var ftpInboundFolder in ftpInboundFolders) { InboundControl inboundCtrl = this.genericMgr.FindAll<InboundControl>("from InboundControl ic where ic.FtpFolder=?", ftpInboundFolder).SingleOrDefault(); FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpInboundFolder, ftpUser, ftpPass); foreach (string vaildFileName in ftp.GetFileList(vaildFilePattern)) { string fileName = filePattern.Replace("*", vaildFileName.Substring(0, vaildFileName.IndexOf("."))); string inFolder = inboundCtrl.InFloder; inFolder = inFolder.Replace("\\", "/"); if (!inFolder.EndsWith("/")) { inFolder += "/"; } if (!Directory.Exists(inFolder)) { Directory.CreateDirectory(inFolder); } try { ftp.Download(inFolder, vaildFileName);//CTL ftp.Download(inFolder, fileName);//DAT //log.Info("Move file from folder: " + localTempFolder + vaildFileName + " to folder: " + localFolder + vaildFileName); File.Copy(inFolder + fileName, localTempFolder + fileName); //if (ftpBackupFolder != null && ftpBackupFolder.Length > 0) //{ // ftp.MovieFile(vaildFileName, ftpBackupFolder); // ftp.MovieFile(fileName, ftpBackupFolder); // //ftp.MovieFile(fileName, ftpControl.FtpBackUp); // //ftp.MovieFile(fileName.Substring(0, fileName.IndexOf("."))+".CTL", ftpControl.FtpBackUp); //} //else //{ ftp.Delete(vaildFileName); ftp.Delete(fileName); //} } catch (Exception ex) { log.Error("Download file:" + vaildFileName, ex); //ftp.MovieFile(fileName, ftpControl.FtpErrorFolder); //ftp.MovieFile(fileName.Substring(0, fileName.IndexOf(".")) + ".CTL", ftpControl.FtpErrorFolder); } } } #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."); }
public void Execute(JobRunContext context) { #region 获取参数 JobDataMap jobDataMap = context.JobDataMap; string inboundDirectoryName = string.Empty; string archiveDirectoryName = string.Empty; string errorDirectoryName = string.Empty; string ftpServer = string.Empty; int ftpPort = 21; string ftpInboundFolder = string.Empty; string ftpUser = string.Empty; string ftpPass = string.Empty; string localTempFolder = string.Empty; if (jobDataMap.ContainKey(INBOUND_DIRECTORY)) { inboundDirectoryName = jobDataMap.GetStringValue(INBOUND_DIRECTORY); } else { throw new BusinessErrorException("not specify inbound directory."); } if (jobDataMap.ContainKey(ARCHIVE_DIRECTORY)) { archiveDirectoryName = jobDataMap.GetStringValue(ARCHIVE_DIRECTORY); } else { throw new BusinessErrorException("not specify archive directory."); } if (jobDataMap.ContainKey(ERROR_DIRECTORY)) { errorDirectoryName = jobDataMap.GetStringValue(ERROR_DIRECTORY); } else { throw new BusinessErrorException("not specify error directory."); } if (jobDataMap.ContainKey(FTP_SERVER)) { ftpServer = jobDataMap.GetStringValue(FTP_SERVER); } else { throw new BusinessErrorException("not ftp server address."); } if (jobDataMap.ContainKey(FTP_PORT)) { ftpPort = jobDataMap.GetIntValue(FTP_PORT); } else { log.Info("using default ftp port 21."); } if (jobDataMap.ContainKey(FTP_INBOUND_FOLDER)) { ftpInboundFolder = jobDataMap.GetStringValue(FTP_INBOUND_FOLDER); } else { throw new BusinessErrorException("not specify ftp inbound folder."); } if (jobDataMap.ContainKey(FTP_USER)) { ftpUser = jobDataMap.GetStringValue(FTP_USER); } else { throw new BusinessErrorException("not specify ftp user."); } if (jobDataMap.ContainKey(FTP_PASSWORD)) { ftpPass = jobDataMap.GetStringValue(FTP_PASSWORD); } else { throw new BusinessErrorException("not specify ftp password."); } if (jobDataMap.ContainKey(LOCAL_TEMP_FOLDER)) { localTempFolder = jobDataMap.GetStringValue(LOCAL_TEMP_FOLDER); } else { throw new BusinessErrorException("not specify local temp folder."); } #endregion #region 下载WO文件 try { #region 初始化本地目录 localTempFolder = localTempFolder.Replace("\\", "/"); if (!localTempFolder.EndsWith("/")) { localTempFolder += "/"; } if (!Directory.Exists(localTempFolder)) { Directory.CreateDirectory(localTempFolder); } inboundDirectoryName = inboundDirectoryName.Replace("\\", "/"); if (!inboundDirectoryName.EndsWith("/")) { inboundDirectoryName += "/"; } if (!Directory.Exists(inboundDirectoryName)) { Directory.CreateDirectory(inboundDirectoryName); } #endregion #region 下载文件 FtpHelper ftp = new FtpHelper(ftpServer, ftpPort, ftpInboundFolder, ftpUser, ftpPass); string[] fileList = ftp.GetFileList(); foreach (string fileName in fileList) { try { ftp.Download(localTempFolder, fileName); log.Info("Move file from folder: " + localTempFolder + fileName + " to folder: " + inboundDirectoryName + fileName); File.Move(localTempFolder + fileName, inboundDirectoryName + fileName); ftp.Delete(fileName); } catch (Exception ex) { log.Error("Download file:" + fileName, ex); } } #endregion } catch (Exception ex) { log.Error("Download files from ftpServer:" + ftpServer, ex); } #endregion string[] inboundFiles = Directory.GetFiles(inboundDirectoryName); if (inboundFiles != null && inboundFiles.Length > 0) { foreach (string inboundFile in inboundFiles) { string fileName = inboundFile.Substring(inboundFile.LastIndexOf("/")); try { ProcessSingleFile(inboundDirectoryName, inboundFile); if (!Directory.Exists(archiveDirectoryName)) { Directory.CreateDirectory(archiveDirectoryName); } if (File.Exists(archiveDirectoryName + fileName)) { File.Delete(archiveDirectoryName + fileName); } File.Move(inboundFile, archiveDirectoryName + fileName); } catch (Exception ex) { log.Error("Unexpected error occour when processing inbound file " + inboundFile, ex); if (!Directory.Exists(errorDirectoryName)) { Directory.CreateDirectory(errorDirectoryName); } if (File.Exists(errorDirectoryName + fileName)) { File.Delete(errorDirectoryName + fileName); } File.Move(inboundFile, errorDirectoryName + fileName); } } } }