`
lovnet
  • 浏览: 6693077 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)

阅读更多

(一). 概述

最近做了个C/S文件下载工具,支持多任务, 多线程和断点续传功能. 其中部分代码是从网上找来的, 自己改了

许多Thread Bug, 并增加多任务, 断点续传等功能.

由于公司具有代码所有权, 不能将源代码共享.自己对比较Asp.net感兴趣, 业余时间自己做了个简单的, 基于

Asp.net2.0的, 目前能够执行对一个文件的下载任务, 但已经实现了多线程, 断点续传功能. 根据需要您可以增加

多任务 功能, 分享一下, 互相学习! 互相借鉴!

时间仓促, 此程序还没有做很多参数方面的优化. 可以作参考用.

(二).运行效果

(三). 代码

1. 核心 DownLoadState.cs 文件代码

1///<summary>
2///Author:[ChengKing(ZhengJian)]
3///Blog:Http://blog.csdn.net/ChengKing
4///注:从网上找了个优秀代码
5///扩展如下功能:
6///1.解决一些线程相关的Bug;
7///2.扩展用控制文件实现断点续传功能.
8///</summary>
9namespaceDownLoadComponent
10{
11///<summary>
12///多线程辅助类(AddbyChengKing)
13///</summary>
14publicclassTask
15{
16string_FromFileName;
17string_ToFileName;
18int_ThreadNum;
19
20publicTask(stringFromFileName,stringToFileName,intThreadNum)
21{
22this._FromFileName=FromFileName;
23this._ToFileName=ToFileName;
24this._ThreadNum=ThreadNum;
25}
26
27publicstringFromFileName
28{
29get
30{
31return_FromFileName;
32}
33set
34{
35_FromFileName=value;
36}
37}
38publicstringToFileName
39{
40get
41{
42return_ToFileName;
43}
44set
45{
46_ToFileName=value;
47}
48}
49publicintThreadNum
50{
51get
52{
53return_ThreadNum;
54}
55set
56{
57_ThreadNum=value;
58}
59}
60}
61
62///<summary>
63///记录下载的字节位置
64///</summary>
65publicclassDownLoadState
66{
67privatestring_FileName;
68
69privatestring_AttachmentName;
70privateint_Position;
71privatestring_RequestURL;
72privatestring_ResponseURL;
73privateint_Length;
74
75privatebyte[]_Data;
76
77publicstringFileName
78{
79get
80{
81return_FileName;
82}
83}
84
85publicintPosition
86{
87get
88{
89return_Position;
90}
91}
92
93publicintLength
94{
95get
96{
97return_Length;
98}
99}
100
101
102publicstringAttachmentName
103{
104get
105{
106return_AttachmentName;
107}
108}
109
110publicstringRequestURL
111{
112get
113{
114return_RequestURL;
115}
116}
117
118publicstringResponseURL
119{
120get
121{
122return_ResponseURL;
123}
124}
125
126
127publicbyte[]Data
128{
129get
130{
131return_Data;
132}
133}
134
135internalDownLoadState(stringRequestURL,stringResponseURL,stringFileName,stringAttachmentName,intPosition,intLength,byte[]Data)
136{
137this._FileName=FileName;
138this._RequestURL=RequestURL;
139this._ResponseURL=ResponseURL;
140this._AttachmentName=AttachmentName;
141this._Position=Position;
142this._Data=Data;
143this._Length=Length;
144}
145
146internalDownLoadState(stringRequestURL,stringResponseURL,stringFileName,stringAttachmentName,intPosition,intLength,ThreadCallbackHandlertch)
147{
148this._RequestURL=RequestURL;
149this._ResponseURL=ResponseURL;
150this._FileName=FileName;
151this._AttachmentName=AttachmentName;
152this._Position=Position;
153this._Length=Length;
154this._ThreadCallback=tch;
155}
156
157internalDownLoadState(stringRequestURL,stringResponseURL,stringFileName,stringAttachmentName,intPosition,intLength)
158{
159this._RequestURL=RequestURL;
160this._ResponseURL=ResponseURL;
161this._FileName=FileName;
162this._AttachmentName=AttachmentName;
163this._Position=Position;
164this._Length=Length;
165}
166
167privateThreadCallbackHandler_ThreadCallback;
168
169//
170internalvoidStartDownloadFileChunk()
171{
172if(this._ThreadCallback!=null)
173{
174this._ThreadCallback(this._RequestURL,this._FileName,this._Position,this._Length);
175}
176}
177
178}
179
180//委托代理线程的所执行的方法签名一致
181publicdelegatevoidThreadCallbackHandler(stringS,strings,intI,inti);
182
183//异常处理动作
184publicenumExceptionActions
185{
186Throw,
187CancelAll,
188Ignore,
189Retry
190}
191
192///<summary>
193///包含Exception事件数据的类
194///</summary>
195publicclassExceptionEventArgs:System.EventArgs
196{
197privateSystem.Exception_Exception;
198privateExceptionActions_ExceptionAction;
199
200privateDownLoadState_DownloadState;
201
202publicDownLoadStateDownloadState
203{
204get
205{
206return_DownloadState;
207}
208}
209
210publicExceptionException
211{
212get
213{
214return_Exception;
215}
216}
217
218publicExceptionActionsExceptionAction
219{
220get
221{
222return_ExceptionAction;
223}
224set
225{
226_ExceptionAction=value;
227}
228}
229
230internalExceptionEventArgs(System.Exceptione,DownLoadStateDownloadState)
231{
232this._Exception=e;
233this._DownloadState=DownloadState;
234}
235}
236
237///<summary>
238///包含DownLoad事件数据的类
239///</summary>
240publicclassDownLoadEventArgs:System.EventArgs
241{
242privateDownLoadState_DownloadState;
243
244publicDownLoadStateDownloadState
245{
246get
247{
248return_DownloadState;
249}
250}
251
252publicDownLoadEventArgs(DownLoadStateDownloadState)
253{
254this._DownloadState=DownloadState;
255}
256
257}
258
259///<summary>
260///支持断点续传多线程下载的类
261///</summary>
262publicclassHttpWebClient
263{
264privatestaticobject_SyncLockObject=newobject();
265
266publicdelegatevoidDataReceiveEventHandler(HttpWebClientSender,DownLoadEventArgse);
267
268publiceventDataReceiveEventHandlerDataReceive;//接收字节数据事件
269
270publicdelegatevoidExceptionEventHandler(HttpWebClientSender,ExceptionEventArgse);
271
272publiceventExceptionEventHandlerExceptionOccurrs;//发生异常事件
273
274privateint_FileLength;//下载文件的总大小
275
276publicstaticArrayListthreads;
277
278publicintFileLength
279{
280get
281{
282return_FileLength;
283}
284}
285
286///<summary>
287///分块下载文件
288///</summary>
289///<paramname="Address">URL地址</param>
290///<paramname="FileName">保存到本地的路径文件名</param>
291///<paramname="ChunksCount">块数,线程数</param>
292publicvoidDownloadFile(stringAddress,stringFileName,intChunksCount)
293{
294intp=0;//position
295ints=0;//chunksize
296stringa=null;
297HttpWebRequesthwrq;
298HttpWebResponsehwrp=null;
299try
300{
301
302hwrq=(HttpWebRequest)WebRequest.Create(this.GetUri(Address));
303//hwrq.Timeout=20000000;
304//if(hwrq.HaveResponse==false)
305//return;
306//hwrq.ProtocolVersion=HttpVersion.Version10;
307//WebProxywp=WebProxy.GetDefaultProxy();
308//hwrq.Proxy=wp;
309hwrq.Method="GET";
310try
311{
312hwrp=(HttpWebResponse)hwrq.GetResponse();
313}
314catch(Exceptione)
315{
316thrownewException(e.Message);
317}
318
319longL=hwrp.ContentLength;
320
321//如果文件太小,就不用分多线程,用一个线程下载即可.(目前控制在800K)
322//if(L<800000)
323//{
324//ChunksCount=1;
325//}
326
327hwrq.Credentials=this.m_credentials;
328
329L=((L==-1)||(L>0x7fffffff))?((long)0x7fffffff):L;//Int32.MaxValue该常数的值为2,147,483,647;即十六进制的0x7FFFFFFF
330
331intl=(int)L;
332
333this._FileLength=l;
334
335boolb=(hwrp.Headers["Accept-Ranges"]!=null&hwrp.Headers["Accept-Ranges"]=="bytes");
336a=hwrp.Headers["Content-Disposition"];//attachment
337if(a!=null)
338{
339a=a.Substring(a.LastIndexOf("filename=")+9);
340}
341else
342{
343a=FileName;
344}
345
346intss=s;
347if(b)
348{
349if(ExistControlFile(FileName))//是否存在文件
350{
351string[]strBlocks=this.ReadInfFromControlFile(FileName).Split(newchar[2]{'\r','\n'});
352for(inti=0;i<strBlocks.Length;i++)
353{
354if(strBlocks[i].Trim().Length!=0&&strBlocks[i].Substring(strBlocks[i].Length-1)=="0")
355{
356string[]strRe
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics