程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> .Net/C#實現支持斷點續傳多線程下載

.Net/C#實現支持斷點續傳多線程下載

編輯:C#入門知識

最近在學習用c#來實現多線程下載,終於讓我google到了些資料,以下是代碼拷貝:
namespace Microshaoft.Utils
{
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Security;
using System.Threading;
using System.Collections.Specialized;

/// <summary>
/// 記錄下載的字節位置
/// </summary>
public class DownLoadState
{
private string _FileName;

private string _AttachmentName;
private int _Position;
private string _RequestURL;
private string _ResponseURL;
private int _Length;

private byte[] _Data;

public string FileName
{
get
{
return _FileName;
}
}

public int Position
{
get
{
return _Position;
}
}

public int Length
{
get
{
return _Length;
}
}


public string AttachmentName
{
get
{
return _AttachmentName;
}
}

public string RequestURL
{
get
{
return _RequestURL;
}
}

public string ResponseURL
{
get
{
return _ResponseURL;
}
}


public byte[] Data
{
get
{
return _Data;
}
}

internal DownLoadState(string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length, byte[] Data)
{
this._FileName = FileName;
this._RequestURL = RequestURL;
this._ResponseURL = ResponseURL;
this._AttachmentName = AttachmentName;
this._Position = Position;
this._Data = Data;
this._Length = Length;
}

internal DownLoadState(string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length, ThreadCallbackHandler tch)
{
this._RequestURL = RequestURL;
this._ResponseURL = ResponseURL;
this._FileName = FileName;
this._AttachmentName = AttachmentName;
this._Position = Position;
this._Length = Length;
this._ThreadCallback = tch;
}

internal DownLoadState(string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length)
{
this._RequestURL = RequestURL;
this._ResponseURL = ResponseURL;
this._FileName = FileName;
this._AttachmentName = AttachmentName;
this._Position = Position;
this._Length = Length;
}

private ThreadCallbackHandler _ThreadCallback;

public HttpWebClient httpWebClient
{
get
{
return this._hwc;
}
set
{
this._hwc = value;
}
}

internal Thread thread
{
get
{
return _thread;
}
set
{
_thread = value;
}
}

private HttpWebClient _hwc;
private Thread _thread;

//
internal void StartDownloadFileChunk()
{
if (this._ThreadCallback != null)
{
this._ThreadCallback(this._RequestURL, this._FileName, this._Position, this._Length);
this._hwc.OnThreadProcess(this._thread);
}
}

}

//委托代理線程的所執行的方法簽名一致
public delegate void ThreadCallbackHandler(string S, string s, int I, int i);

//異常處理動作
public enum ExceptionActions
{
Throw,
CancelAll,
Ignore,
Retry
}

/// <summary>
/// 包含 Exception 事件數據的類
/// </summary>
public class ExceptionEventArgs : System.EventArgs
{
private System.Exception _Exception;
private ExceptionActions _ExceptionAction;

private DownLoadState _DownloadState;

public DownLoadState DownloadState
{
get
{
return _DownloadState;
}
}

public Exception Exception
{
get
{
return _Exception;
}
}

public ExceptionActions ExceptionAction
{
get
{
return _ExceptionAction;
}
set
{
_ExceptionAction = value;
}
}

internal ExceptionEventArgs(System.Exception e, DownLoadState DownloadState)
{
this._Exception = e;
this._DownloadState = DownloadState;
}
}

/// <summary>
/// 包含 DownLoad 事件數據的類
/// </summary>
public class DownLoadEventArgs : System.EventArgs
{
private DownLoadState _DownloadState;

public DownLoadState DownloadState
{
get
{
return _DownloadState;
}
}

public DownLoadEventArgs(DownLoadState DownloadState)
{
this._DownloadState = DownloadState;
}

}

public class ThreadProcessEventArgs : System.EventArgs
{
private Thread _thread;

public Thread thread
{
get
{
return this._thread;
}
}

public ThreadProcessEventArgs(Thread thread)
{
this._thread = thread;
}

}

/// <summary>
/// 支持斷點續傳多線程下載的類
/// </summary>
public class HttpWebClient
{
private static object _SyncLockObject = new object();

public delegate void DataReceiveEventHandler(HttpWebClient Sender, DownLoadEventArgs e);

public event DataReceiveEventHandler DataReceive; //接收字節數據事件

public delegate void ExceptionEventHandler(HttpWebClient Sender, ExceptionEventArgs e);

public event ExceptionEventHandler ExceptionOccurrs; //發生異常事件

public delegate void ThreadProcessEventHandler(HttpWebClient Sender, ThreadProcessEventArgs e);

public event ThreadProcessEventHandler ThreadProcessEnd; //發生多線程處理完畢事件


private int _FileLength; //下載文件的總大小

public int FileLength
{
get
{
return _FileLength;
}
}

/// <summary>
/// 分塊下載文件
/// </summary>
/// <param name="Address">URL 地址</param>
/// <param name="FileName">保存到本地的路徑文件名</param>
/// <param name="ChunksCount">塊數,線程數</param>
public void DownloadFile(string Address, string FileName, int ChunksCount)
{
int p = 0; // position
int s = 0; // chunk size
string a = null;
HttpWebRequest hwrq;
HttpWebResponse hwrp = null;
try
{
hwrq = (HttpWebRequest) WebRequest.Create(this.GetUri(Address));
hwrp = (HttpWebResponse) hwrq.GetResponse();
long L = hwrp.ContentLength;

hwrq.Credentials = this.m_credentials;

L = ((L == -1) || (L > 0x7fffffff)) ? ((long) 0x7fffffff) : L; //Int32.MaxValue 該常數的值為 2,147,483,647; 即十六進制的 0x7FFFFFFF

int l = (int) L;

this._FileLength = l;

// 在本地預定空間(竟然在多線程下不用先預定空間)
// FileStream sw = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
// sw.Write(new byte[l], 0, l);
// sw.Close();
// sw = null;

bool b = (hwrp.Headers["Accept-Ranges"] != null & hwrp.Headers["Accept-Ranges"] == "bytes");
a = hwrp.Headers["Content-Disposition"]; //attachment
if (a != null)
{
a = a.Substring(a.LastIndexOf("filename=") +

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved