private void _Download() { try { if (UnCompeleteSize <= 0) { IsCompleted = true; BlockEvents.OnProgress(100, 100); BlockEvents.OnCompleted(); Request.Abort(); return; } Log.Debug(BlockIndex.ToString() + "号文件块开始下载."); Request.Timeout = Timeout.Infinite; Request.Accept = "*/*"; Request.AllowAutoRedirect = true; Request.AddRange(StartPosition + _CompletedPosition); Log.Debug(BlockIndex.ToString() + "号块正在获取网络回应."); var response = (HttpWebResponse)Request.GetResponse(); Log.Debug(BlockIndex.ToString() + "号块正在获取流."); Stream stream = response.GetResponseStream(); if (stream == null) { IsCompleted = true; BlockEvents.OnProgress(100, 100); BlockEvents.OnCompleted(); Request.Abort(); Log.Debug("无法获取到WebRequest请求的相应文件流~"); return; } Log.Debug(BlockIndex.ToString() + "号块正在创建暂存文件."); using (var filestream = new FileStream(FilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) { var buffersize = DownloadState.BufferSize; var buffer = new byte[buffersize]; Log.Debug(BlockIndex.ToString() + "号块初始化完毕."); try { while (UnCompeleteSize > 0) { Log.Debug(BlockIndex.ToString() + "号块开始从" + _CompletedPosition.ToString() + "处读取流数据."); int receiveCount = stream.Read(buffer, 0, (int)buffersize); Log.Debug(BlockIndex.ToString() + "号块本次读取流数据长度为" + receiveCount.ToString() + "字节."); if (receiveCount <= 0) { var sb = new StringBuilder(); sb.Append("发生下载阻塞" + "\t"); sb.Append("BlockIndex:" + BlockIndex.ToString() + "\t"); sb.Append("StartPosition:" + (StartPosition + CompletedPosition).ToString() + "\t"); sb.Append("BufferSize:" + BlockSize.ToString() + "\t"); sb.Append("ReceiveCount:" + receiveCount.ToString()); Log.Debug(sb.ToString()); Thread.Sleep(5000); continue; } int availbleReceiveCount = (UnCompeleteSize < receiveCount ? UnCompeleteSize : receiveCount); filestream.Write(buffer, 0, availbleReceiveCount); filestream.Flush(); _CompletedPosition += availbleReceiveCount; Log.Debug(BlockIndex.ToString() + "号块已完成" + _CompletedPosition.ToString() + "/" + this.BlockSize.ToString() + "字节的下载."); BlockEvents.CompletedBytesCount += availbleReceiveCount; if (UnCompeleteSize <= 0 || DownloadState.StopDownload) { break; } } buffer = null; response.Close(); stream.Close(); if (DownloadState.StopDownload) { return; } Log.Debug(BlockIndex.ToString() + "号文件块下载完成."); this.IsCompleted = true; BlockEvents.OnCompleted(); this.Request.Abort(); filestream.Close(); } catch (Exception err) { buffer = null; response.Close(); stream.Close(); filestream.Close(); BlockEvents.OnException(err); this.Request.Abort(); } } } catch (Exception err) { BlockEvents.OnException(err); this.Request.Abort(); } }
private void DownloadFile(TransferParameter parameter) { try { string logstr = "下载任务开始,正在创建临时目录."; Log.Debug(logstr); string[] tempList = Directory.GetDirectories(Logger.TemporaryDirectory); DirectoryInfo di = null; if (tempList.Length > 0) { foreach (string item in tempList) { di = new DirectoryInfo(item); if (di.LastWriteTime <= DateTime.Now.AddMonths(-6)) { FileUtil.DeleteDirectory(item); } } } logstr = "正在创建本地保存目录."; Log.Debug(logstr); string localDir = Path.GetDirectoryName(parameter.LocalFile); if (!string.IsNullOrEmpty(localDir)) { di = new DirectoryInfo(localDir); if (!di.Exists) { di.Create(); } } logstr = "正在初始化..."; Log.Debug(logstr + "\r\n" + parameter.ToString()); DownloadState.BlockList.Clear(); var currentApply = new Apply(); DownloadState.BlockList = DownloadRequst.GetBlocks(parameter, ref currentApply); Log.Debug(currentApply.ToString()); DownloadState.AllowAppend = false; DownloadState.StopDownload = false; DownloadState.HasFinished = false; DownloadState.IsCompleted = false; DownloadState.OutputDebugLog = true; BlockEvents.CompletedBytesCount = 0; BlockEvents.TotalBytes = currentApply.FileSize; ServicePointManager.DefaultConnectionLimit = 100; var historyApply = new Apply(); var historyFileItem = new FileItem(); var historyparameter = new TransferParameter(); if (DownloadHistory.Exists(parameter.RemoteFile, ref historyFileItem, ref historyApply, ref historyparameter, false) && DownloadState.CurrentApply.Equals(historyApply) && !File.Exists(parameter.LocalFile)) { logstr = "已恢复下载."; Log.Debug(logstr); DownloadState.AllowAppend = true; historyFileItem.HasBreaked = true; if (string.IsNullOrEmpty(historyFileItem.BreakInfo)) { historyFileItem.BreakInfo = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } else { historyFileItem.BreakInfo += ";" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } DownloadHistory.Update(parameter.RemoteFile, historyFileItem); } else if (DownloadHistory.Exists(parameter.RemoteFile, ref historyFileItem, ref historyApply, ref historyparameter, true) && parameter.Equals(historyparameter) && File.Exists(historyparameter.LocalFile)) { var fileInfo = new FileInfo(parameter.LocalFile); if (fileInfo.Length == DownloadState.CurrentApply.FileSize && fileInfo.LastWriteTime == DownloadState.CurrentApply.LastModified) { logstr = parameter.TransferUrl + "此前已完成下载."; Log.Debug(logstr); DownloadState.HasFinished = true; BlockEvents.CompletedBytesCount = fileInfo.Length; BlockEvents.OnProgress(100, 100); BlockEvents.OnCompleted(); return; } } if (!DownloadState.AllowAppend && !DownloadState.HasFinished) { DownloadHistory.Insert(parameter.RemoteFile); logstr = "已将本次下载任务追加到历史."; Log.Debug(logstr); } if (BlockEvents.Begining != null) { BlockEvents.Begining(parameter.LocalFile); } logstr = "开始处理分块"; Log.Warn(logstr); foreach (Blocks item in DownloadState.BlockList) { item.ReStore(); item.Download(); logstr += item.ToString() + "\r\n"; Log.Debug(logstr); } Log.Info("文件下载完毕"); } catch (Exception err) { BlockEvents.OnException(err); } }