SSIS 學習之旅 FTP訪問類

這章把腳本任務訪問FTP的方法 全部給大家。

控件的使用大家如果有不懂得可以看下我之前的文章。
第一章:SSIS 學習之旅 第一個SSIS 示例(一)(上)

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

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

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

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

?

        #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 學習之旅 到這里就結束了。希望對大家的工作有所幫助吧。

?

轉載于:https://www.cnblogs.com/yinsq/p/5482810.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/271812.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/271812.shtml
英文地址,請注明出處:http://en.pswp.cn/news/271812.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Spring Cloud Feign 使用Apache的HTTP Client替換Feign原生httpclient

http 連接池能提升性能 http 的背景原理 a. 兩臺服務器建立 http 連接的過程是很復雜的一個過程&#xff0c;涉及到多個數據包的交換&#xff0c;并且也很耗時間。 b. Http 連接需要的 3 次握手 4 次分手開銷很大&#xff0c;這一開銷對于大量的比較小的 http 消息來說更大。…

Java容器坐標起點_Java的屏幕坐標是以像素為單位,容器的左下角被確定為坐標的起點...

【單選題】【單選題】【單選題】class A{ int x1; void func1(int x1){ this.x1 x1; } } 關于上述程序,說法錯誤的是( )【單選題】瀏覽器的作用是( )。【判斷題】構建大學生心理危機預警及干預工作機制,更好地幫助有嚴重心理問題的學生度過心理難關,及早預防、及時疏導、有效干…

自媒體工具:文本內容轉音頻文件實用小工具

目錄 ?編輯 1、軟件介紹 2、軟件技術框架 3、使用說明 4、核心代碼文件 5、注意事項 1、軟件介紹 文本內容轉轉音頻文件小工具&#xff0c;采用C#編程語言&#xff0c;基于Framework4.5開發&#xff0c;主要采用百度語音識別SDK&#xff0c;實現了在線文本內容轉音頻文件的功能…

IDEA 創建 SpringCloud項目-多項目方式

SpringCloud 雖然可以用多模塊化的方式來創建&#xff0c;但是&#xff0c;SpirngCloud本身就是為分布式而準備的&#xff0c;如果使用多模塊的話&#xff0c;那就是一個項目&#xff0c;偏離了分布式的概念。所以工程上還是常用多項目的方式&#xff0c;這樣才可以分開布署各個…

php位運算重要嗎,PHP位運算的用途

下面為大家帶來一篇PHP位運算的用途。現在就分享給大家&#xff0c;也給大家做個參考。一起過來看看吧在實際應用中可以做用戶權限的應用我這里說到的權限管理辦法是一個普遍采用的方法&#xff0c;主要是使用到”位運行符”操作&#xff0c;& 位與運算符、| 位或運行符。參…

盤點6款實用的文件對比工具,你都用過嗎?

??作者主頁&#xff1a;IT技術分享社區 ??作者簡介&#xff1a;大家好,我是IT技術分享社區的博主&#xff0c;從事C#、Java開發九年&#xff0c;對數據庫、C#、Java、前端、運維、電腦技巧等經驗豐富。 ??個人榮譽&#xff1a; 數據庫領域優質創作者&#x1f3c6;&#x…

aggregations 詳解1(概述)

aggregation分類 aggregations —— 聚合&#xff0c;提供了一種基于查詢條件來對數據進行分桶、計算的方法。有點類似于 SQL 中的 group by 再加一些函數方法的操作。 聚合可以嵌套&#xff0c;由此可以組成復雜的操作&#xff08;Bucketing聚合可以包含sub-aggregation&#…

IDEA開發中,類的頭位置生成作者時間信息

點擊 File > Settings > File and Code Templates > Class按照圖中步驟添加如下信息 #if (${PACKAGE_NAME} && ${PACKAGE_NAME} ! "")package ${PACKAGE_NAME};#end #parse("File Header.java") /** * Author WangZeyu * Date ${…

提現接口網站 php,API提現接口

>獲取提現積分的類型&#xff0c;在后臺可以設置某種積分可被提現&#xff0c;此處獲取的數據為可提現積分的類型~~~[api]get:/index.php/accounts/Apipoint/member_withdrawal_listint:type 0#是否智能限制提現積分類型&#xff0c;0&#xff1a;不智能&#xff0c;1&#…

數據庫:PostgreSQL 和 MySQL對比

比較版本&#xff1a;PostgreSQL 11 VS MySQL5.7&#xff08;innodb引擎&#xff09; Oracle官方社區版版權情況&#xff1a;PostgreSQL 11&#xff08;免費開源&#xff09;、MySQL5.7 Oracle官方社區版&#xff08;免費開源&#xff09; 1. CPU限制 PGSQL沒有CPU核心數限制&a…

C#獲取當前系統磁盤符、系統目錄、桌面等

1.獲取方式如下 Environment.SpecialFolder中定義了許多常用的目錄 //獲取當前系統磁盤符方法1&#xff0c;返回&#xff1a;C: string path Environment.GetEnvironmentVariable("systemdrive"); //獲取當前系統磁盤符方法2,返回&#xff1a;C: string path Envir…

MAC電腦常用效率工具推薦

??作者主頁&#xff1a;IT技術分享社區 ??作者簡介&#xff1a;大家好,我是IT技術分享社區的博主&#xff0c;從事C#、Java開發九年&#xff0c;對數據庫、C#、Java、前端、運維、電腦技巧等經驗豐富。 ??個人榮譽&#xff1a; 數據庫領域優質創作者&#x1f3c6;&#x…

Java String類型轉換成Date日期類型

//格式化數據 SimpleDateFormat sdf new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format sdf.format(new Date()); System.out.println(format);//String格式的數據轉化成Date格式 String timeStr "2019-07-09 03:34:56 "; Date parse sdf.pa…

docker php 安裝swoole,swoole(1)使用docker安裝swoole環境

1.下載鏡像pull php 鏡像docker pull php:7.3-alpine3.8創建容器docker run -it --name test php:7.3-alpine3.8 sh2.進入容器安裝swoole# 安裝依賴的第三方包echo http://mirrors.ustc.edu.cn/alpine/v3.7/main > /etc/apk/repositories && \echo http://mirrors.u…

插件書寫示例

正常模態框代碼 <!DOCTYPE html> <html lang"en"> <head><meta charset"utf-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" content"widthdevice-wid…

MAC電腦8款常用設計工具推薦

目錄 1、Sketch 3 2、Affinity Designer 3、Framer 4、PaintCode 5、Hype 3 Professional 6、Pixave 2 7、Iconjar 8、Sip for Mac and iPhone 1、Sketch 3 Sketch是完全滿足我上面4條選擇條件的一款UI設計工具&#xff0c;前文第50頁已經提到過相關內容。 2、Affinity Designe…

matlab the installer cannot read,MATLAB安裝 The installer cannot read the mwinstall.dll… | 學步園...

前提&#xff1a;安裝前的文件 的 目錄 不需含有 中文安裝MATLAB7時彈出以下警告對話框&#xff0c;顯示“ The installer cannot read the mwinstall.dll file, This is probably due to a CD reader which can only read files with an eight.three naming convention. Pleas…

專家觀點:即使在云中 硬件同樣至關重要

英特爾最近發布了新一代企業級CPU。第四代E5和E7CPU確實有些與眾不同之處&#xff1a;更多的緩存、更多的內核、更快更多的RAM。但是大家沒有發現這款產品還有一個特別的地方。 也許一切都與云有關&#xff0c;我們已經告別以前的硬件。你可能認為IT命令結構級別越高&#xff0…

matlab怎么安裝compiler,關于MATLAB中compiler配置問題

按照大家的方法進行了系統配置&#xff0c;下載安裝了SDK7.1&#xff0c;可運行mex setup之后還是一直彈出No supported SDK or compiler was found on this computer.Welcome to mex -setup. This utility will help you set upa default compiler. For a list of supported…

計算機硬件:內存條的基礎知識筆記

在電腦硬件中&#xff0c;CPU、顯卡、內存均三者是重中之重&#xff0c;所以我們在選擇這些核心硬件一定要慎重。今天給大家分享一下關于的電腦內存基礎知識&#xff0c;讓更多的裝機朋友們可以更好的學習內存相關知識。 史上最易懂的電腦內存基礎知識 內存條的基本概念&#x…