多线程断点续传研究之一

本人最近应网友之邀,在一篇文章的基础上去实现一个多线程断点续传下载文件的程序。但是在编写的过程中,发现问题多多。

 

原文地址为:

http://dev.csdn.net/develop/article/64/64877.shtm

 

知道通过HttpWebRequest就可以进行多线程断点下载,是我不用考虑从Socket写起。

 

对于一个多线程断点续传程序,我大致认为只要考虑如下几点问题就行了。

1. 下载数据可以从给定位置进行;

2. 可以进行分块下载;

3. 记录下载位置,以供下次重新下载的时候使用。

 

通过对原文的阅读来看,发现以上问题的前两个已经实现。这样会使需要附加的操作会更简单些。

 

为了能记录每个线程下载的位置,我借用了最简单的方式,就是xml方式。

 

以下就是多线程下载的主文件内容:

//--------------------------- Download File class ---------------------------------------

//---------------------------------------------------------------------------------------

//---File:          clsNewDownloadFile

//---Description:   The multi-download file class file using HttpWebRequest class

//---Author:        Knight

//---Date:          Aug.4, 2006

//---------------------------------------------------------------------------------------

//---------------------------{Download File class}---------------------------------------

 

namespace DownloadFile

{

    using System;

    using System.IO;

    using System.Net;

    using System.Text;

    using System.Security;

    using System.Threading;

    using System.Collections.Specialized;

    using System.Diagnostics;

    using System.Data;

 

    /// <summary>

    /// Download file info

    /// </summary>

    public class DownloadFileInfo

    {

        private string _RequestURL;

        private string _ResponseURL;

        private string _FileName;

        private int _TotalSize;

        private int _BlockLeftSize;

 

        public string RequestURL

        {

            get{ return _RequestURL; }

        }

 

        public string ResponseURL

        {

            get{ return _ResponseURL; }

        }

        public string FileName

        {

            get{ return _FileName; }

        }

        public int TotalSize

        {

            get{ return _TotalSize;}

            set{ _TotalSize = value;}

        }

        public int BlockLeftSize

        {

            get{ return _BlockLeftSize;}

            set{ _BlockLeftSize = value;}

        }

 

        public DownloadFileInfo(

            string RequestURL,

            string ResponseURL,

            string FileName,

            int TotalSize,

            int BlockLeftSize )

        {

            this._RequestURL = RequestURL;

            this._ResponseURL = ResponseURL;

            this._FileName = FileName;

            this._TotalSize = TotalSize;

            this._BlockLeftSize = BlockLeftSize;

        }

 

        public DownloadFileInfo(

            string RequestURL,

            string ResponseURL,

            string FileName ):

            this( RequestURL, ResponseURL, FileName, 0, 0 )

        {

        }

    }

 

    /// <summary>

    /// Download event arguments

    /// </summary>

    public class DownLoadEventArgs : System.EventArgs

    {

        private DownloadFileInfo _DownloadFileInfo;

        private int _Position;

        private int _ReadCount;

        private int _ThreadNO;

 

        public DownloadFileInfo DownFileInfo

        {

            get{ return _DownloadFileInfo; }

        }

        public int StartPosition

        {

            get{ return _Position; }

        }

        public int ReadCount

        {

            get{ return _ReadCount; }

        }

        public int ThreadNO

        {

            get{ return _ThreadNO; }

        }

 

        public DownLoadEventArgs(

            DownloadFileInfo DownFileInfo,

            int nStartPostion,

            int nReadCount,

            int nThreadNO )

        {

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值