/// <summary> /// Http方式接收一个区块 /// </summary> //private void ReceiveHttp(object threadId) private void ReceiveHttp(object thread) { //int ThreadId = (int)threadId; ThreadInfo currentThread = (ThreadInfo)thread; //string filename = fileNames[ThreadId]; // 线程临时文件 byte[] buffer = new byte[bufferSize]; // 接收缓冲区 int readSize = 0; // 接收字节数 //FileStream fs = new FileStream(filename, System.IO.FileMode.Create); FileStream fs = new FileStream(currentThread.TmpFileName, System.IO.FileMode.Create); Stream ns = null; try { //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); //request.AddRange(StartPos[ThreadId], StartPos[ThreadId] + fileSize[ThreadId]); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(currentThread.Url); request.AddRange(currentThread.StartPosition, currentThread.StartPosition + currentThread.FileSize); ns = request.GetResponse().GetResponseStream(); readSize = ns.Read(buffer, 0, bufferSize); while (readSize > 0) { fs.Write(buffer, 0, readSize); //sumOfThreads[currentThread.ThreadId] += readSize; readSize = ns.Read(buffer, 0, bufferSize); //lock (lockPercent) // { receiveSize += readSize; double percent = (double)receiveSize / (double)fileSizeAll * 100; OnDownloadingPercent(percent); //触发下载进度事件 //} } } catch //这里不用处理异常,如果等待超时了,会自动继续等待到可以下载为止 { //throw ex; } finally { if (fs != null) { fs.Close(); } if (ns != null) { ns.Close(); } } //threadStatus[currentThread.ThreadId] = true; currentThread.ThreadStatus = true; }
/// <summary> /// 方式多线程下载一个文件 /// </summary> /// <param name="srcFileUrl">文件地址</param> /// <param name="destFileName">保存全名</param> /// <param name="maxThreadNum">线程数</param> /// <param name="rowIndex">任务索引</param> //public void DownloadFile(string srcFileUrl, string destFileName, int maxThreadNum, int rowIndex) //{ // if (srcFileUrl.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase)) // { // DownloadHttpFile(srcFileUrl, destFileName, maxThreadNum, rowIndex); // } //} /// <summary> /// Http方式多线程下载一个文件 /// </summary> /// <param name="srcFileUrl">文件地址</param> /// <param name="destFileName">保存全名</param> /// <param name="maxThreadNum">线程数</param> /// <param name="rowIndex">任务索引</param> //public void DownloadFile(string srcFileUrl, string destFileName, int maxThreadNum, int rowIndex) public void DownloadFile(List<string> srcFileUrl, string destFileName, int maxThreadNum, int rowIndex) { OnGetStartTime(DateTime.Now); //Url = srcFileUrl; Urls = srcFileUrl; DestFileName = destFileName; RowIndex = rowIndex; threadNum = maxThreadNum;//多少个线程下载 receiveSize = 0; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Urls[0]); fileSizeAll = request.GetResponse().ContentLength; request.Abort(); OnUpdateSrcFileSize(0, fileSizeAll); //初始化多线程,每个线程平均分配文件大小,剩余部分由最后一个线程完成 //threadStatus = new bool[threadNum]; //fileNames = new string[threadNum]; //StartPos = new int[threadNum]; //fileSize = new int[threadNum]; //threadinfos = new ThreadInfo[threadNum]; int filethread = (int)fileSizeAll / threadNum; int filethreadEnd = filethread + (int)fileSizeAll % threadNum; for (int i = 0; i < threadNum; i++) { //sumOfThreads.Add(0); //threadStatus[i] = false; ThreadInfo currentThread = new ThreadInfo(); //threadinfos[i].ThreadId = i; //threadinfos[i].ThreadStatus = false; //threadinfos[i].TmpFileName = string.Format("{0}_{1}.tmp", DestFileName, i); //threadinfos[i].Url = Urls[i % Urls.Count]; currentThread.ThreadId = i; currentThread.ThreadStatus = false; currentThread.TmpFileName = string.Format("{0}_{1}.tmp", DestFileName, i); currentThread.Url = Urls[i % Urls.Count]; //fileNames[i] = string.Format("{0}_{1}.tmp", DestFileName, i); if (i < threadNum - 1) { //StartPos[i] = filethread * i; //fileSize[i] = filethread - 1; currentThread.StartPosition = filethread * i; currentThread.FileSize = filethread - 1; } else { //StartPos[i] = filethread * i; //fileSize[i] = filethreadEnd - 1; currentThread.StartPosition = filethread * i; currentThread.FileSize = filethreadEnd - 1; } threadinfos.Add(currentThread); } // 定义并启动线程数组 for (int i = 0; i < threadinfos.Count; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(ReceiveHttp), threadinfos[i]); } //启动合并线程 ThreadPool.QueueUserWorkItem(new WaitCallback(MergeFile), null); }
/// <summary> /// 方式多线程下载一个文件 /// </summary> /// <param name="srcFileUrl">文件地址</param> /// <param name="destFileName">保存全名</param> /// <param name="maxThreadNum">线程数</param> /// <param name="rowIndex">任务索引</param> //public void DownloadFile(string srcFileUrl, string destFileName, int maxThreadNum, int rowIndex) //{ // if (srcFileUrl.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase)) // { // DownloadHttpFile(srcFileUrl, destFileName, maxThreadNum, rowIndex); // } //} /// <summary> /// Http方式多线程下载一个文件 /// </summary> /// <param name="srcFileUrl">文件地址</param> /// <param name="destFileName">保存全名</param> /// <param name="maxThreadNum">线程数</param> /// <param name="rowIndex">任务索引</param> //public void DownloadFile(string srcFileUrl, string destFileName, int maxThreadNum, int rowIndex) public void DownloadFile(List <string> srcFileUrl, string destFileName, int maxThreadNum, int rowIndex) { OnGetStartTime(DateTime.Now); //Url = srcFileUrl; Urls = srcFileUrl; DestFileName = destFileName; RowIndex = rowIndex; threadNum = maxThreadNum;//多少个线程下载 receiveSize = 0; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Urls[0]); fileSizeAll = request.GetResponse().ContentLength; request.Abort(); OnUpdateSrcFileSize(0, fileSizeAll); //初始化多线程,每个线程平均分配文件大小,剩余部分由最后一个线程完成 //threadStatus = new bool[threadNum]; //fileNames = new string[threadNum]; //StartPos = new int[threadNum]; //fileSize = new int[threadNum]; //threadinfos = new ThreadInfo[threadNum]; int filethread = (int)fileSizeAll / threadNum; int filethreadEnd = filethread + (int)fileSizeAll % threadNum; for (int i = 0; i < threadNum; i++) { //sumOfThreads.Add(0); //threadStatus[i] = false; ThreadInfo currentThread = new ThreadInfo(); //threadinfos[i].ThreadId = i; //threadinfos[i].ThreadStatus = false; //threadinfos[i].TmpFileName = string.Format("{0}_{1}.tmp", DestFileName, i); //threadinfos[i].Url = Urls[i % Urls.Count]; currentThread.ThreadId = i; currentThread.ThreadStatus = false; currentThread.TmpFileName = string.Format("{0}_{1}.tmp", DestFileName, i); currentThread.Url = Urls[i % Urls.Count]; //fileNames[i] = string.Format("{0}_{1}.tmp", DestFileName, i); if (i < threadNum - 1) { //StartPos[i] = filethread * i; //fileSize[i] = filethread - 1; currentThread.StartPosition = filethread * i; currentThread.FileSize = filethread - 1; } else { //StartPos[i] = filethread * i; //fileSize[i] = filethreadEnd - 1; currentThread.StartPosition = filethread * i; currentThread.FileSize = filethreadEnd - 1; } threadinfos.Add(currentThread); } // 定义并启动线程数组 for (int i = 0; i < threadinfos.Count; i++) { ThreadPool.QueueUserWorkItem(new WaitCallback(ReceiveHttp), threadinfos[i]); } //启动合并线程 ThreadPool.QueueUserWorkItem(new WaitCallback(MergeFile), null); }