SSIS 學習之旅 FTP訪問類

這章把腳本任務訪問FTP的方法 所有給你們。html

控件的使用你們若是有不懂得能夠看下我以前的文章。
第一章:SSIS 學習之旅 第一個SSIS 示例(一)(上)服務器

第二章:SSIS 學習之旅 第一個SSIS 示例(二)post

第三章:SSIS 學習之旅 數據同步學習

第四章:SSIS 學習之旅 FTP文件傳輸-FTP任務ui

第五章:SSIS 學習之旅 FTP文件傳輸-腳本任務url

 

        #region 鏈接FTP服務器
        /// <summary>  
        /// 鏈接FTP服務器
        /// </summary>  
        /// <param name="FtpServerIP">FTP鏈接地址</param>  
        /// <param name="FtpRemotePath">指定FTP鏈接成功後的當前目錄, 若是不指定即默認爲根目錄</param>  
        public string FTPHelper(string FtpServerIP, string FtpRemotePath)
        {
            string ftpURI = "ftp://" + FtpServerIP + "/" + FtpRemotePath + "/";
            return ftpURI;
        }

        #endregion

        #region 文件上傳FTP服務器
        /// <summary>
        /// 上傳
        /// </summary>
        /// <param name="FilePathPendingAndName">文件詳細路徑</param>
        /// <param name="FTPUrl">FTPUrl</param>
        /// <param name="FTP_UserName">用戶名</param>
        /// <param name="FTP_PWD">密碼</param>
        public void Upload(string FilePathPendingAndName, string FTPUrl, string FTP_UserName, string FTP_PWD)
        {
            FileInfo fileInf = new FileInfo(FilePathPendingAndName);
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPUrl + fileInf.Name));
            reqFTP.Credentials = new NetworkCredential(FTP_UserName, FTP_PWD);
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.KeepAlive = false;
            reqFTP.UseBinary = true;
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            FileStream fs = fileInf.OpenRead();
            try
            {
                Stream strm = reqFTP.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

      #endregion


        

        #region 下載文件
        /// <summary>
        /// 下載文件
        /// </summary>
        /// <param name="filePath">本地路徑</param>
        /// <param name="fileName">文件名</param>
        /// <param name="ftpUrl">FTP連接路徑</param>
        /// <param name="FTP_UserName">用戶名</param>
        /// <param name="FTP_PWD">密碼</param>
        public void Download(string filePath, string fileName, string ftpUrl, string FTP_UserName, string FTP_PWD)
        {
            try
            {
                FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
                FtpWebRequest reqFTP;

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUrl + fileName));
                reqFTP.Credentials = new NetworkCredential(FTP_UserName, FTP_PWD);
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


     #region 刪除文件
        /// <summary> 
        /// 刪除文件 
        /// </summary> 
        public void Delete(string fileName)
        {
            try
            {
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
                reqFTP.KeepAlive = false;
                string result = String.Empty;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                long size = response.ContentLength;
                Stream datastream = response.GetResponseStream();
                StreamReader sr = new StreamReader(datastream);
                result = sr.ReadToEnd();
                sr.Close();
                datastream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
     #endregion

    #region 獲取當前目錄下文件列表(不包括文件夾)
        /// <summary>
        /// 獲取當前目錄下文件列表(不包括文件夾)
        /// </summary>
        /// <param name="url">鏈接FTP服務器地址</param>
        /// <param name="ftpUserName">用戶名</param>
        /// <param name="ftpPassword">密碼</param>
        /// <returns></returns>
        public string[] GetFileList(string url, string ftpUserName, string ftpPassword)
        {
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

                reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

                WebResponse response = reqFTP.GetResponse();

                StreamReader reader = new StreamReader(response.GetResponseStream());

                string line = reader.ReadLine();

                string FileName = "";
                while (line != null)
                {

                    if (line.IndexOf("<DIR>") == -1)
                    {
                        FileName = "";
                        FileName =  Regex.Match(line, @"(?<=IN)([.\S\s]*)(?=csv)", RegexOptions.IgnoreCase).Value.ToString() ;
                        if (FileName.Trim() != "")
                        {
                            FileName = "IN" + FileName + "csv";
                            result.Append(FileName + "|");
                        }
                    }
                    line = reader.ReadLine();
                }
                //result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return result.ToString().Split('|');
        }

        #endregion

        #region 判斷當前目錄下指定的文件是否存在
        /// <summary>  
        /// 判斷當前目錄下指定的文件是否存在  
        /// </summary>  
        /// <param name="RemoteFileName">遠程文件名</param> 
        public bool FileExist(string FTPUrl, string RemoteFileName, string FTP_UserName, string FTP_PWD)
        {

            string FileName = "IN_NORMAL_" + Regex.Match(RemoteFileName, @"(?<=IN_NORMAL_)([.\S\s]*)(?=csv)", RegexOptions.IgnoreCase).Value.ToString() + "csv"; 

            string[] fileList = GetFileList(FTPUrl, FTP_UserName, FTP_PWD);
            foreach (string str in fileList)
            {
                if (str.Trim()==FileName.Trim())
                {
                    return true;
                }
            }
            return false;
        }
        #endregion


        #region 更改文件名
        /// <summary>  
        /// 更改文件名  
        /// </summary> 
        /// <param name="currentFilename">現有文件名稱</param>
        /// <param name="newDirectory">新的文件名稱</param>
        /// <param name="FTPUrl">FTPUrl</param>
        /// <param name="FTP_UserName">用戶名</param>
        /// <param name="FTP_PWD">密碼</param>
        public void ReName(string currentFilename, string newFilename, string FTPUrl, string FTP_UserName, string FTP_PWD)
        {
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPUrl + currentFilename));
                reqFTP.Method = WebRequestMethods.Ftp.Rename;
                reqFTP.RenameTo = newFilename;
                reqFTP.UseBinary = true;
                
                reqFTP.Credentials = new NetworkCredential(FTP_UserName, FTP_PWD);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                //File.Move()
                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            { }
        }

        #endregion

        #region 移動文件夾
        /// <summary>
        /// 移動文件夾
        /// </summary>
        /// <param name="currentFilename">現有文件名稱</param>
        /// <param name="newDirectory">新的文件名稱</param>
        /// <param name="FTPUrl">FTPUrl</param>
        /// <param name="FTP_UserName">用戶名</param>
        /// <param name="FTP_PWD">密碼</param>
        public void MovieFile(string currentFilename, string newDirectory,string FTPUrl, string FTP_UserName, string FTP_PWD)
        {
            ReName(currentFilename, newDirectory, FTPUrl, FTP_UserName, FTP_PWD);
        }
        #endregion


    #region 建立文件夾 
        /// <summary> 
        /// 建立文件夾 
        /// </summary>  
        public void MakeDir(string dirName)
        {
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName));
                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            { }
        }
     #endregion

    #region 獲取指定文件大小 
        /// <summary> 
        /// 獲取指定文件大小 
        /// </summary> 
        public long GetFileSize(string filename)
        {
            FtpWebRequest reqFTP;
            long fileSize = 0;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));
                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                fileSize = response.ContentLength;
                ftpStream.Close();
                response.Close();
            }
            catch (Exception ex)
            { }
            return fileSize;
        }
    #endregion

 

至此 SSIS 學習之旅 到這裏就結束了。但願對你們的工做有所幫助吧。spa

相關文章
相關標籤/搜索