c# Ftp下載程式原始碼解析

來源:互聯網
上載者:User

標籤:io   ar   os   使用   for   sp   檔案   資料   div   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace 網路操作
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void menuStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {


        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(textBox1.Text);//能夠是ftpserver的絕對路徑也能夠是相對路徑
            //URI 能夠是相對的也能夠是絕對的。假設 URI 的形式為 "ftp://contoso.com/%2fpath"(%2f 是逸出字元“/”),則該 URI 是絕對的,並且當前檔案夾為 /path。可是,假設 URI 的形式為 "ftp://contoso.com/path",首先 .NET Framework 登入到 FTP server(使用由 Credentials 屬性設定的username和password),然後將當前檔案夾設定為 <UserLoginDirectory>/path。
            //uri不是url所以第一個文字框中應該輸入ftp://url/cftea.txt
            ftpRequest.Credentials = new NetworkCredential(textBox2.Text, textBox3.Text);
            //您必須擁有server的有效username和password,或者server必須同意匿名登入。能夠通過設定 Credentials 屬性來指定用於串連server的憑據,也能夠將它們包括在傳遞給 Create 方法的 URI 的 UserInfo 部分中。假設 URI 中包括 UserInfo 資訊,則使用指定的username和password資訊將 Credentials 屬性設定為新的網路憑據。
            //為基於password的身分識別驗證方案(如基本、簡要、NTLM 和 Kerberos 身分識別驗證)提供憑據。
            //此類不支援基於公開金鑰的驗證方法,如安全通訊端層 (SSL) client身分識別驗證
            //public NetworkCredential(string userName,string password)
            
            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            //若要訪問 FTP 特定的屬性,必須將此方法返回的 WebResponse 對象強制轉換為 FtpWebResponse。
            //返回 FTP server響應。
            //FtpWebResponse封裝檔案傳輸通訊協定 (FTP) server對請求的響應。


            Stream data = ftpResponse.GetResponseStream();  //通過響應對象獲得響應流
            //擷取流,該流用於讀取來自server的響應的體
            //本程式中 檢索包括從 FTP server上發送的響應資料的流
           //get方法的 返回值一個 Stream,包括響應的體。
            //GetResponseStream 方法從請求的 Internet 資源返回資料流。
            string str = textBox1.Text.Substring(textBox1.Text.LastIndexOf("/"), textBox1.Text.Length - textBox1.Text.LastIndexOf("/"));
            //函數參數1和參數2在 startIndex 處開頭、長度為 length 的子字串等效的一個字串假設找到該字元,則為 value 的從零開始的索引位置;假設未找到,則為 -1。
            //str.length當前字串中字元的數量。
            //  報告指定 Unicode 字元在此執行個體中的最後一個匹配項的從零開始的索引位置。(本例中為最後一個/的下標位置)
            //最後提取出的字串為textBox1.Text去除/之前的字元
            string SavePath = str;
            if (File.Exists(SavePath))
            {
                File.Delete(str);
            }
            byte[] buffer = new byte[4096];
            FileStream stream = new FileStream(SavePath, FileMode.Create);
            //使用指定的路徑和建立模式初始化 FileStream 類的新執行個體。由 .NET Compact Framework 支援。
            //摘要: 
            //指定作業系統應建立新檔案。 假設檔案已存在,它將被覆蓋。 這須要 System.Security.Permissions.FileIOPermissionAccess.Write
             //許可權。 FileMode.Create 等效於這種請求:假設檔案不存在,則使用 System.IO.FileMode.CreateNew;否則使用
            //System.IO.FileMode.Truncate。 假設該檔案已存在但為隱藏檔案,則將引發 System.UnauthorizedAccessException異常。
            int count = 0;
            do
            {
                count = data.Read(buffer, 0, buffer.Length); //讀取從ftp中獲得的響應的資料流對象
                //public abstract int Read(byte[] buffer,int offset,int count)第一個參數為位元組第二個為位移量第三個為讀取的字元數
                if (count > 0)
                {
                    stream.Write(buffer, 0, count);//參照read  
                    //這個是檔案流對象通過從ftp中獲得的資料流開始讀取字元然後寫入檔案流來儲存
                }
            } while (count > 0);
            //讀入緩衝區中的總位元組數。 假設當前可用的位元組數沒有請求的位元組數那麼多,
            //則總位元組數可能小於請求的位元組數,或者假設已到達流的末尾,則為零 (0)
            ftpResponse.Close();//上一句是打算將字串讀完,這一句是FtpWebResponse物件流程關閉
            stream.Close();   //stream流關閉
        }


        }
  }

c# Ftp下載程式原始碼解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.