/// <summary> /// 保存断点下载的数据碎片 /// </summary> /// <param name="downloadItemModel"></param> private void SaveProcessFragmentsData(DownloadItemModel downloadItemModel) { if (!File.Exists(downloadItemModel.FragmentsFileSaveName)) { File.Create(downloadItemModel.FragmentsFileSaveName).Dispose(); } File.WriteAllText(downloadItemModel.FragmentsFileSaveName, downloadItemModel.ModelToJsonString()); }
/// <summary> /// 开始下载文件 /// </summary> /// <param name="downloadurl">下载地址</param> /// <param name="saveFileName">文件保存位置。包含文件的保存路径和完整的文件名</param> /// <param name="downloadingCallback">下载中的回调</param> /// <param name="downloadedFinshCallback">下载完成后的回调</param> /// <param name="downloadErrorCallBack">下载错误回调</param> /// <param name="isResetDownload">标记是否强制重新下载</param> /// <param name="data">下载任务的附加数据</param> /// <param name="mtag">下载任务的附加数据</param> /// <returns></returns> public HTTPRequest DownLoad(string downloadurl, string saveFileName, BlankActionForDownloadItemModel downloadingCallback, BlankActionForDownloadItemModel downloadedFinshCallback, BlankAction downloadErrorCallBack, bool isResetDownload = false, string data = "", string mtag = "") { if (!Directory.Exists(saveFileName)) { Directory.CreateDirectory(Path.GetDirectoryName(saveFileName)); } if (m_downloadHttpRequests.ContainsKey(downloadurl)) { if (m_downloadItemModels.ContainsKey(downloadurl)) { DownloadItemModel downloadItemModel = m_downloadItemModels[downloadurl]; downloadItemModel.DownloadingCallback = downloadingCallback; downloadItemModel.DownloadedFinshCallback = downloadedFinshCallback; } // 下载中 return(m_downloadHttpRequests[downloadurl]); } else { // 未下载中 // 读取 断点数据 DownloadItemModel downloadItemModel = ReadProcessFragmentsData(saveFileName, isResetDownload); if (string.IsNullOrEmpty(downloadItemModel.ID)) { downloadItemModel.ID = DateTime.Now.ToFileTime().ToString(); } downloadItemModel.Data = data; downloadItemModel.Tag = mtag; downloadItemModel.LastDownloadTimeSpan = Time.realtimeSinceStartup; downloadItemModel.DownloadUrl = downloadurl; downloadItemModel.FileSaveName = saveFileName; downloadItemModel.DownloadingCallback = downloadingCallback; downloadItemModel.DownloadedFinshCallback = downloadedFinshCallback; HTTPRequest httpRequest = new HTTPRequest(new Uri(downloadurl), (req, rep) => { if (rep != null) { if (downloadItemModel.DownloadSize <= 0) { string contentLength = rep.GetFirstHeaderValue("content-length"); downloadItemModel.DownloadSize = long.Parse(contentLength); } } switch (req.State) { case HTTPRequestStates.Initial: break; case HTTPRequestStates.Queued: break; case HTTPRequestStates.Processing: { downloadItemModel.IsDownloadFinsh = false; ProgressDownloadData(saveFileName, rep, downloadItemModel); // 下载中的回调 if (downloadItemModel.DownloadingCallback != null) { try { downloadingCallback(downloadItemModel); } catch (Exception exception) { Debug.LogError("DownloadURL:" + downloadurl + exception.Message + exception.Source + exception.TargetSite + downloadItemModel.ToString()); throw; } } } break; case HTTPRequestStates.Finished: if (rep != null && rep.IsSuccess) { ProgressDownloadData(saveFileName, rep, downloadItemModel); if (rep.IsStreamingFinished) { UpdateDownloadFinshState(downloadItemModel); // 下载完成后回调 if (downloadedFinshCallback != null) { try { downloadedFinshCallback(downloadItemModel); } catch (Exception exception) { Debug.LogError("DownloadURL:" + downloadurl + exception.Message + exception.Source + exception.TargetSite + downloadItemModel.ToString()); throw; } } Log.I("下载完成"); } StopDownLoad(downloadItemModel.DownloadUrl); } break; case HTTPRequestStates.Aborted: { // 下载 终止 Log.I(string.Format(" 下载任务 {0} 终止 ", downloadurl)); NotificationManager.Instance.PostNotification(DownLoadManagerNotificationConst.DOWN_LOAD_MANAGER_TASK_STOP, downloadurl, DownLoadManagerNotificationConst.DOWN_LOAD_MANAGER_FILTER); } break; case HTTPRequestStates.Error: case HTTPRequestStates.ConnectionTimedOut: case HTTPRequestStates.TimedOut: { if (downloadErrorCallBack != null) { downloadErrorCallBack(); } // 下载错误 Log.I(string.Format(" 下载任务 {0} 错误 ", downloadurl)); NotificationManager.Instance.PostNotification(DownLoadManagerNotificationConst.DOWN_LOAD_MANAGER_TASK_ERROR, downloadurl, DownLoadManagerNotificationConst.DOWN_LOAD_MANAGER_FILTER); } break; } }) { IsKeepAlive = true, DisableCache = true, UseStreaming = true, StreamFragmentSize = HTTPResponse.MinBufferSize * 100, ConnectTimeout = TimeSpan.FromSeconds(5), Timeout = TimeSpan.FromSeconds(5) }; if (downloadItemModel.IsDownloadFinsh) { Log.I("已经下载过该文件" + downloadItemModel.ModelToJsonString()); UpdateDownloadFinshState(downloadItemModel); // 下载完成后回调 if (downloadedFinshCallback != null) { try { downloadedFinshCallback(downloadItemModel); } catch (Exception exception) { Debug.LogError("DownloadURL:" + downloadurl + exception.Message + exception.Source + exception.TargetSite + downloadItemModel); throw; } } return(httpRequest); } if (downloadItemModel.DownloadedSize > 0) { httpRequest.SetRangeHeader((int)downloadItemModel.DownloadedSize); httpRequest.Tag = downloadItemModel.DownloadedSize; Log.I(" 开始断点下载 " + downloadItemModel.ModelToJsonString()); } else { DataCacheManager.Instance.AppendList(downloadItemModel, DOWN_LOAD_MANAGER_MODULE_NAME); } httpRequest.Send(); m_downloadItemModels.Add(downloadurl, downloadItemModel); m_downloadHttpRequests.Add(downloadurl, httpRequest); return(httpRequest); } }