C#實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載的方法
本文實(shí)例講述了C#實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Text;
using System.Net;
namespace simpleDemo
{
class Program
{
/// <summary>
/// 下載文件保留字
/// </summary>
public static string PERSIST_EXP = ".cdel";
/// <summary>
public static void Main(string[] args)
{
string path = "D:\\aa.txt";
string ec = getFileEncoding(path, "GB2312");
print("coding: " + ec);
// string content = fileReader(path, Encoding.GetEncoding(ec));
// print(content);
//fileWriter(path, "測(cè)試內(nèi)容11", Encoding.GetEncoding(ec));
string url = "http://www.XXX.com/20120920172200024.flv";
string path1 = "D:\\aa1.flv";
download(url, path1);
//gapDownload(url, path1);
//t(url);
}
public static void t(string url) {
HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);
//WebResponse response = httpClient.CreateGetHttpResponse(url, 3000, null, null);
try {
WebResponse response = request.GetResponse();
WebHeaderCollection headers = response.Headers;
print(response.ContentLength);
request = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);
request.AddRange(11); //設(shè)置Range值
WebResponse response1 = request.GetResponse();
print(response1.ContentLength);
foreach (string key in headers)
{
print(key + "----- " + headers.Get(key));
}
string disposition = headers.Get("Content-Disposition");
print(disposition);
}catch(Exception e){
print(e.Message);
}
//string fileName = disposition.Substring(disposition.IndexOf("\""));
//print(fileName);
}
public static void download(string url, string path) {
if (File.Exists(path))
{
print("文件己存在!是否重新下載?");
return;
}
else {
path = path + PERSIST_EXP;
simpleDownload(url,path);//開(kāi)始下載
}
}
/// <summary>
/// 下載網(wǎng)絡(luò)資源(支持?jǐn)帱c(diǎn)續(xù)傳)
/// </summary>
/// <param name="url"></param>
/// <param name="path"></param>
public static void simpleDownload(string url, string path)
{
HttpWebRequest request = httpClient.getWebRequest(url, 0);
WebResponse response = null;
FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
long lStartPos = writer.Length; ;//當(dāng)前文件大小
long currentLength = 0;
long totalLength = 0;//總大小
if (File.Exists(path))//斷點(diǎn)續(xù)傳
{
response = request.GetResponse();
long sTotal = response.ContentLength;
if (sTotal == lStartPos) {
close(writer);
File.Move(path, path.Replace(PERSIST_EXP, ""));
print("下載完成!");
return;
}
request = httpClient.getWebRequest(url, (int)lStartPos);
//設(shè)置Range值
writer.Seek(lStartPos, SeekOrigin.Begin);//指針跳轉(zhuǎn)
response = request.GetResponse();
totalLength = response.ContentLength + lStartPos; //總長(zhǎng)度
currentLength = lStartPos; //當(dāng)前長(zhǎng)度
}
else
{
response = request.GetResponse();
totalLength = response.ContentLength;
}
Stream reader = response.GetResponseStream();
byte[] buff = new byte[1024];
int c = 0; //實(shí)際讀取的字節(jié)數(shù)
while ((c = reader.Read(buff, 0, buff.Length)) > 0)
{
currentLength += c;
writer.Write(buff, 0, c);
progressBar(currentLength, totalLength);//進(jìn)度條
writer.Flush();
}
close(writer);
if (currentLength == totalLength)
{
File.Move(path, path.Replace(PERSIST_EXP, ""));
print("下載完成!");
}
if (reader != null)
{
reader.Close();
reader.Dispose();
response.Close();
}
}
private static void close(FileStream writer)
{
if (writer != null)
{
writer.Close();
writer.Dispose();
}
}
/// <summary>
/// 進(jìn)度條
/// </summary>
/// <param name="currentLength">當(dāng)前長(zhǎng)度</param>
/// <param name="totalLength">總長(zhǎng)度</param>
public static void progressBar(Object currentLength, Object totalLength)
{
double aaa = System.Convert.ToDouble(currentLength);
double bbb = System.Convert.ToDouble(totalLength);
print(currentLength + "/" + totalLength + "__" + (aaa / bbb).ToString("0.00 %"));
}
/// <summary>
/// 系統(tǒng)輸出
/// </summary>
/// <param name="obj"></param>
public static void print(Object obj){
Console.WriteLine(obj);
}
public static void printStr(string[] str)
{
foreach (string d in str)
{
print(d);
}
}
/// <summary>
/// 文件寫(xiě)
/// </summary>
/// <param name="path">文件路徑</param>
/// <param name="content">要寫(xiě)入的內(nèi)容</param>
public static void fileWriter(string path,string content,Encoding encoding)
{
if (File.Exists(path))
{
StreamWriter sw = new StreamWriter(path, true, encoding);
sw.WriteLine(content);
sw.Flush();
sw.Close();
}
}
/// <summary>
/// 讀文件,返回內(nèi)容
/// </summary>
/// <param name="path">文件路徑</param>
/// <param name="enCoding">默認(rèn)編碼格式</param>
/// <returns></returns>
public static string fileReader(string path,Encoding enCoding) {
StringBuilder sb = new StringBuilder();
if(enCoding == null){
enCoding = Encoding.Default;
}
//讀取文件
StreamReader sr = new StreamReader(path, enCoding);
string s = "";
while ((s = sr.ReadLine()) != null)
{
sb.AppendLine(s);
}
if(sr != null)
sr.Close();
return sb.ToString();
}
/// <summary>
/// 獲取文件編碼格式
/// </summary>
/// <param name="path">文件路徑</param>
/// <param name="defaultEncoding">默認(rèn)編碼</param>
/// <returns></returns>
public static string getFileEncoding(string path, string defaultEncoding) {
string ed = defaultEncoding;
if (File.Exists(path)) {
FileStream fs = new FileStream(path, FileMode.Open);
ed = GetEncoding(fs, defaultEncoding);
if (fs != null)
fs.Close();
}
return ed;
}
/// <summary>
/// 取得一個(gè)文本文件流的編碼方式。
/// </summary>
/// <param name="stream">文本文件流。</param>
/// <param name="defaultEncoding">默認(rèn)編碼方式。當(dāng)該方法無(wú)法從文件的頭部取得有效的前導(dǎo)符時(shí),將返回該編碼方式。</param>
/// <returns></returns>
public static string GetEncoding(FileStream stream, string defaultEncoding)
{
string targetEncoding = defaultEncoding;
if (stream != null && stream.Length >= 2)
{
//保存文件流的前4個(gè)字節(jié)
byte byte1 = 0;
byte byte2 = 0;
byte byte3 = 0;
byte byte4 = 0;
//保存當(dāng)前Seek位置
long origPos = stream.Seek(0, SeekOrigin.Begin);
stream.Seek(0, SeekOrigin.Begin);
int nByte = stream.ReadByte();
byte1 = Convert.ToByte(nByte);
byte2 = Convert.ToByte(stream.ReadByte());
if (stream.Length >= 3)
{
byte3 = Convert.ToByte(stream.ReadByte());
}
if (stream.Length >= 4)
{
byte4 = Convert.ToByte(stream.ReadByte());
}
//根據(jù)文件流的前4個(gè)字節(jié)判斷Encoding
//Unicode {0xFF, 0xFE};
//BE-Unicode {0xFE, 0xFF};
//UTF8 = {0xEF, 0xBB, 0xBF};
if (byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe
{
targetEncoding = Encoding.BigEndianUnicode.BodyName;
}
if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode
{
targetEncoding = Encoding.Unicode.BodyName;
}
if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8
{
targetEncoding = Encoding.UTF8.BodyName;
}
//恢復(fù)Seek位置
stream.Seek(origPos, SeekOrigin.Begin);
}
return targetEncoding;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO;
namespace simpleDemo
{
/// <summary>
/// 公用 Http 請(qǐng)求類(lèi)
/// </summary>
class httpClient
{
/// <summary>
/// 獲取基礎(chǔ)WebRequest
/// </summary>
/// <param name="url">請(qǐng)求地址</param>
/// <param name="lStartPos">請(qǐng)求的開(kāi)始位置</param>
/// <returns></returns>
public static HttpWebRequest getWebRequest(string url, int lStartPos)
{
HttpWebRequest request = null;
try
{
request = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);
request.AddRange(lStartPos); //設(shè)置Range值
}
catch (Exception ex)
{
Program.print(ex.Message);
}
return request;
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn)
- C# FileStream實(shí)現(xiàn)多線(xiàn)程斷點(diǎn)續(xù)傳
- C# 文件下載之?dāng)帱c(diǎn)續(xù)傳實(shí)現(xiàn)代碼
- C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- c#實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能示例分享
- C#實(shí)現(xiàn)支持?jǐn)帱c(diǎn)續(xù)傳多線(xiàn)程下載客戶(hù)端工具類(lèi)
- C#服務(wù)端圖片打包下載實(shí)現(xiàn)代碼解析
- c# 實(shí)現(xiàn)文件上傳下載功能的實(shí)例代碼
- C#怎樣實(shí)現(xiàn)文件下載斷點(diǎn)續(xù)傳
相關(guān)文章
C# http系列之以form-data方式上傳多個(gè)文件及鍵值對(duì)集合到遠(yuǎn)程服務(wù)器
這篇文章主要介紹了C# http系列之以form-data方式上傳多個(gè)文件及鍵值對(duì)集合到遠(yuǎn)程服務(wù)器,需要的朋友可以參考下2019-08-08
利用Aspose.Cells和Excel模板導(dǎo)出統(tǒng)計(jì)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了利用Aspose.Cells和Excel模板導(dǎo)出復(fù)雜的統(tǒng)計(jì)數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
c#實(shí)現(xiàn)51單片機(jī)頻率計(jì)的代碼分享(數(shù)字頻率計(jì)設(shè)計(jì))
c#實(shí)現(xiàn)51單片機(jī)頻率計(jì)的代碼分享,大家參考使用吧2013-12-12
C# async/await任務(wù)超時(shí)處理的實(shí)現(xiàn)
本文主要介紹了C# async/await任務(wù)超時(shí)處理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
C# WinForm制作登錄界面的實(shí)現(xiàn)步驟
本文主要介紹了C# WinForm制作登錄界面的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

