C# Http方式下载文件到本地类改进版
生活随笔
收集整理的這篇文章主要介紹了
C# Http方式下载文件到本地类改进版
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在上文基礎(chǔ)上增加了遠程文件是否存在和本地文件是否存在的判斷。
類代碼:
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; // 獲取已經(jīng)下載的長度if (startPosition >= remoteFileLength){System.Console.WriteLine("本地文件長度" + startPosition + "已經(jīng)大于等于遠程文件長度" + remoteFileLength);writeStream.Close();return false;}else{writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件寫入位置定位 }}else{writeStream = new FileStream(localfile, FileMode.Create);// 文件不保存創(chuàng)建一個文件startPosition = 0;} try{HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打開網(wǎng)絡連接if (startPosition > 0){myRequest.AddRange((int)startPosition);// 設(shè)置Range值,與上面的writeStream.Seek用意相同,是為了定義遠程文件讀取位置 }Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服務器請求,獲得服務器的回應數(shù)據(jù)流byte[] btArray = new byte[512];// 定義一個字節(jié)數(shù)據(jù),用來向readStream讀取內(nèi)容和向writeStream寫入內(nèi)容int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向遠程文件讀第一次long currPostion=startPosition;while (contentSize > 0)// 如果讀取長度大于零則繼續(xù)讀 {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);// 繼續(xù)向遠程文件讀取 }//關(guān)閉流 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);// 打開網(wǎng)絡連接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");}} }?
轉(zhuǎn)載于:https://www.cnblogs.com/xiandedanteng/p/7079322.html
總結(jié)
以上是生活随笔為你收集整理的C# Http方式下载文件到本地类改进版的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鸟哥Linux私房菜知识点总结3到5章
- 下一篇: 夺命雷公狗---linux NO:17