C# Http方式下載文件到本地類改進版

在上文基礎上增長了遠程文件是否存在和本地文件是否存在的判斷。服務器

類代碼:網絡

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

namespace ConsoleTest
{
    class HttpDldFile
    {
       /// <summary>
       /// Http方式下載文件
       /// </summary>
       /// <param name="url">http地址</param>
       /// <param name="localfile">本地文件</param>
       /// <returns></returns>
        public bool Download(string url,string localfile)
        {
            bool flag = false;
            long startPosition = 0; // 上次下載的文件起始位置
            FileStream writeStream; // 寫入本地文件流對象

            long remoteFileLength = GetHttpLength(url);// 取得遠程文件長度
            System.Console.WriteLine("remoteFileLength=" + remoteFileLength);
            if (remoteFileLength==745)
            {
                System.Console.WriteLine("遠程文件不存在.");
                return false;
            }

            // 判斷要下載的文件夾是否存在
            if (File.Exists(localfile))
            {

                writeStream = File.OpenWrite(localfile);             // 存在則打開要下載的文件
                startPosition = writeStream.Length;                  // 獲取已經下載的長度

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

                    return false;
                }
                else
                {
                    writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件寫入位置定位
                }
            }
            else
            {
                writeStream = new FileStream(localfile, FileMode.Create);// 文件不保存建立一個文件
                startPosition = 0;
            }            


            try
            {
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打開網絡鏈接

                if (startPosition > 0)
                {
                    myRequest.AddRange((int)startPosition);// 設置Range值,與上面的writeStream.Seek用意相同,是爲了定義遠程文件讀取位置
                }


                Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服務器請求,得到服務器的迴應數據流


                byte[] btArray = new byte[512];// 定義一個字節數據,用來向readStream讀取內容和向writeStream寫入內容
                int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向遠程文件讀第一次

                long currPostion=startPosition;

                while (contentSize > 0)// 若是讀取長度大於零則繼續讀
                {
                    currPostion+=contentSize;
                    int percent = (int)(currPostion*100/ remoteFileLength);
                    System.Console.WriteLine("percent=" + percent + "%");

                    writeStream.Write(btArray, 0, contentSize);// 寫入本地文件
                    contentSize = readStream.Read(btArray, 0, btArray.Length);// 繼續向遠程文件讀取
                }

                //關閉流
                writeStream.Close();
                readStream.Close();

                flag = true;        //返回true下載成功
            }
            catch (Exception)
            {
                writeStream.Close();
                flag = false;       //返回false下載失敗
            }

            return flag;
        }

        // 從文件頭獲得遠程文件的長度
        private static long GetHttpLength(string url)
        {
            long length=0;

            try
            {
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);// 打開網絡鏈接
                HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();

                if (rsp.StatusCode == HttpStatusCode.OK)
                {
                    length = rsp.ContentLength;// 從文件頭獲得遠程文件的長度
                }

                rsp.Close();
                return length;
            }
            catch(Exception e){
                return length;
            }
                       
        }
    }
}

測試代碼:測試

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

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpDldFile df = new HttpDldFile();
            df.Download("http://files.cnblogs.com/files/xiandedanteng/Convertor20170624.zip","C:\\test\\Convertor20170624.zip");
        }
    }
}
相關文章
相關標籤/搜索