C# Ftp方式下載文件(無用戶認證方式,支持斷點續傳)


類代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO; 

namespace ConsoleTest
{
    class FtpDlder
    {
        public void download(String url, String localFile)
        {
            FtpWebRequest remoteFileLenReq; // 此請求是為了獲取遠程文件長度
            FtpWebRequest remoteFileReadReq;// 此請求是為了讀取文件
            Stream readStream = null;       // 讀取流
            FileStream writeStream = null;  // 寫本地文件流

            try
            {
                writeStream = new FileStream(localFile, FileMode.Append);
                long startPosition=writeStream.Length;// 讀出本地文件已有長度

                // 下面代碼目的是取遠程文件長度
                remoteFileLenReq = (FtpWebRequest)FtpWebRequest.Create(url);
                remoteFileLenReq.UseBinary = true;
                remoteFileLenReq.ContentOffset = 0;
                remoteFileLenReq.Method = WebRequestMethods.Ftp.GetFileSize;
                FtpWebResponse rsp = (FtpWebResponse)remoteFileLenReq.GetResponse();
                long totalByte = rsp.ContentLength;
                rsp.Close();

                if (startPosition >= totalByte)
                {
                    System.Console.WriteLine("本地文件長度" + startPosition + "已經大於等於遠程文件長度" + totalByte);
                    writeStream.Close();

                    return;
                }

                // 初始化讀取遠程文件請求
                remoteFileReadReq = (FtpWebRequest)FtpWebRequest.Create(url);
                remoteFileReadReq.UseBinary = true;
                remoteFileReadReq.KeepAlive = false;
                remoteFileReadReq.ContentOffset = startPosition;
                remoteFileReadReq.Method = WebRequestMethods.Ftp.DownloadFile;
                FtpWebResponse response = (FtpWebResponse)remoteFileReadReq.GetResponse();
                readStream = response.GetResponseStream();

                long downloadedByte = startPosition;
                int bufferSize = 512;
                byte[] btArray = new byte[bufferSize];
                int contentSize = readStream.Read(btArray, 0, btArray.Length);

                while (contentSize > 0)
                {
                    downloadedByte += contentSize;
                    int percent = (int)(downloadedByte * 100 / totalByte);
                    System.Console.WriteLine("percent=" + percent + "%");

                    writeStream.Write(btArray, 0, contentSize);
                    contentSize = readStream.Read(btArray, 0, btArray.Length);
                }
                readStream.Close();
                writeStream.Close();
                response.Close();
                return;
            }
            catch (Exception)
            {
                return;

            }
            finally
            {
                if (readStream != null)
                {
                    readStream.Close();
                }
                if (writeStream != null)
                {
                    writeStream.Close();
                }
            }  
        }
    }
}

 

使用示例:

        static void Main(string[] args)
        {
            // Ftp下載測試,無用戶身份測試
            FtpDlder fd = new FtpDlder();
            fd.download("ftp://192.168.0.109/jump.jpg", "c:\\asd\\jump.jpg");
        }

 

以上代碼參考了 http://blog.csdn.net/jiankunking/article/details/50017009 的代碼,在此向原作者表示感謝。

2017年6月28日16:07:53


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM