public void AsyncDownloadList(List <string> list, string remoteDir, string localDir, Action <DownloadTask> onFinish, List <string> exts = null) { lock (_pendingTasks) { int count = list.Count; string url; for (int i = 0; i < count; ++i) { url = list[i]; DownloadTask task = new DownloadTask(); task.file = url; task.storagePath = CombineUrl(localDir, url); task.url = CombineUrl(remoteDir, url); task.onFinish = onFinish; if (!_pendingTasks.Contains(task)) { _pendingTasks.Enqueue(task); } DownloadFileTransferInfo info = _transferMgr.GetDownloadFileInfo(task.file); task.md5 = info.md5; Debug.Log("<color='#ff0000'>[正在下载文件: " + task.url + "</color>"); } if (_downloadThread.isWaitting) { _downloadThread.Notify(); } } }
/// <summary> /// 断点续传下载 /// </summary> /// <param name="task"></param> private void DownloadFromBreakPoint(DownloadTask task) { try { Uri uri = new Uri(task.url); _currentTaskFileName = task.file; // Debug.Log("Download1"); //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); //// Debug.Log("Download2"); //request.Timeout = TIME_OUT; //request.ReadWriteTimeout = TIME_OUT; //HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //// Debug.Log("Download3"); //long totalLength = response.ContentLength; //response.Close(); //request.Abort(); //Debug.LogWarning(totalLength); DownloadFileTransferInfo dfi = _transferMgr.GetDownloadFileInfo(_currentTaskFileName); long totalLength = dfi.size; long receivedLength = 0L; long toDownloadLength = totalLength; if (File.Exists(task.storagePath)) { //FileInfo fileinfo = new FileInfo(tempFileName); //if (fileinfo.Exists) //{ // receivedLength = fileinfo.Length; // toDownloadLength = totalLength - receivedLength; //} using (FileStream fileStream = new FileStream(task.storagePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { receivedLength = fileStream.Length; toDownloadLength = totalLength - receivedLength; fileStream.Close(); } if (receivedLength != dfi.receivedSize) { CLogger.Log(string.Format("DownloadThread::DownloadFromBreakPoint() - break point save receive size is wrong for file[{0}], saveSize={1}, fileSize={2}", _currentTaskFileName, dfi.receivedSize, receivedLength)); } } task.fileLength = totalLength; task.receivedLength = receivedLength; _currentTaskTotalBytes = totalLength; _currentTaskReceivedBytes = receivedLength; bool transferOkay = true; if (toDownloadLength > 0L) { CLogger.Log("DownloadThread::DownloadFromBreakPoint() - start http download, The request url is [" + uri + "] with range [" + receivedLength + "," + totalLength + "]"); HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(uri); request2.Timeout = kTimeOut; request2.KeepAlive = true; request2.ReadWriteTimeout = kTimeOut; request2.AddRange((int)receivedLength, (int)totalLength); HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse(); transferOkay = this.ReadBytesFromResponse(task, response2); response2.Close(); request2.Abort(); } if (transferOkay) { this.OnDownloadFinished(task, null); } } catch (Exception ex) { CLogger.LogError("DownloadThread::DownloadFromBreakPoint() - ex: " + ex.Message + ",stackTrack:" + ex.StackTrace); this.OnDownloadFinished(task, ex); } }