/// <summary> /// 批量下载 判断完成后 需要支持EasyThread.Stop /// 开线程 /// </summary> /// <param name="list">所有需要下载的内容</param> /// <param name="callback">当前文件本地大小、当前文件服务器大小、所有文件本地大小、所有文件服务器大小、当前文件</param> /// <param name="errorCallback"></param> public void BatchDownload(List <DownloadUnit> list, Action <Size, DownloadUnit, bool> callback, System.Action <DownloadUnit> errorCallback = null) { //Debug.Log("1"); EasyThread downloadThread = null; downloadThread = new EasyThread(() => { download(list, callback, downloadThread, errorCallback); downloadThread.Stop(); }); downloadThread.Start(); }
/// <summary> /// 单个下载 /// 开线程 /// </summary> /// <param name="downUnit"></param> /// <param name="precentCallback">下载进度回调</param> public void SingleDownload(DownloadUnit downUnit, System.Action <long, long, bool> callback, System.Action <DownloadUnit> errorCallback = null) { if (File.Exists(downUnit.FullPath)) { if (callback != null) { callback(1, 1, true); } return; } EasyThread downloadThread = null; downloadThread = new EasyThread(() => { download(downUnit, callback, downloadThread, errorCallback); downloadThread.Stop(); }); downloadThread.Start(); }
//private Dictionary<DownloadUnit, OSSFile> faillist; /// <summary> /// 批量下载 /// </summary> /// <param name="list"></param> /// <param name="callback"></param> public void BatchOSSDownload(Dictionary <DownloadUnit, OSSFile> list, Action <bool> callback) { Dictionary <DownloadUnit, OSSFile> faillist = new Dictionary <DownloadUnit, OSSFile>(); int cureentDownCount = 0; int fallCount = 0; //下载完成的个数统计 用于判断是否全部完成 int i = 0; foreach (var file in list) { //Debug.Log("StartDownload::::" + file.Key.downUrl + " " + file.Value.endpoint + " " + file.Key.fileName + " " + file.Key.size); Loom.QueueOnMainThread((() => { WriteIntoTxt(i + "StartDownload::::" + file.Key.downUrl + " " + file.Value.endpoint + " " + file.Key.fileName + " " + file.Key.size, "list"); i++; })); } int j = 0; foreach (var file in list) { EasyThread et = null; et = new EasyThread((() => { OSSdownload(file.Key, file.Value, (b => { if (b) { Loom.QueueOnMainThread((() => { WriteIntoTxt(j + "DownloadSuccess::::" + file.Key.downUrl + " " + file.Value.endpoint + " " + file.Key.fileName + " " + file.Key.size, "down"); j++; })); // Debug.Log("DownloadSuccess::::" + file.Key.downUrl + " " + file.Value.endpoint + " " + file.Key.fileName + " " + file.Key.size); //HttpManager.Instance.DownLoadcurSize += float.Parse(file.Key.size); } else { fallCount++; faillist.Add(file.Key, file.Value); Debug.LogError("失败:::::::" + file.Key.downUrl + " " + file.Value.endpoint + " " + file.Key.fileName + " " + file.Key.size); Loom.QueueOnMainThread((() => { WriteIntoTxt(j + "DownloadFail::::" + file.Key.downUrl + " " + file.Value.endpoint + " " + file.Key.fileName + " " + file.Key.size, "down"); j++; })); // if (float.Parse(file.Key.size) <= 0 || file.Key.size == null) // { // //HttpManager.Instance.DownLoadcurSize += float.Parse(file.Key.size); // if (cureentDownCount == list.Count) // { // if (callback != null) // { // callback(fallCount <= 0); // } // } // } // else // { // fallCount++; // faillist.Add(file.Key, file.Value); // Debug.LogError("失败:::::::" + file.Key.downUrl + " " + file.Value.endpoint + " " + file.Key.fileName + " " + file.Key.size); // } } cureentDownCount++; Debug.LogWarning("总共 " + list.Count + " 当前 " + cureentDownCount); HttpManager.Instance.DownloadPercent((float)cureentDownCount / (float)list.Count); if (cureentDownCount == list.Count) { callback(fallCount <= 0); } et.Stop(); })); }), ThreadPriority.Highest); et.Start(); } ; }
/// <summary> /// 下载 /// </summary> /// <param name="downUnit"></param> /// <param name="callback"></param> private void download(DownloadUnit downUnit, System.Action <long, long, bool> callback, EasyThread downloadThread, System.Action <DownloadUnit> errorCallback = null) { //打开上次下载的文件 long startPos = 0; //将文件的后缀名改为临时文件名 .temp string tempfilename = downUnit.fileName.Replace(Path.GetExtension(downUnit.fileName), ".temp"); string tempFile = downUnit.savePath + "/" + tempfilename; string InstallFile = downUnit.savePath + "/" + downUnit.fileName; //Debug.LogError("文件路径 :" + InstallFile + " /文件名称/ :" + downUnit.fileName + " /服务器的md5/ :" + downUnit.md5); long totalSize = GetWebFileSize(downUnit.downUrl); //float totalSize; //float.TryParse(downUnit.size, out totalSize); //Debug.Log(totalSize); FileStream fs = null; //若此文件已经存在 则直接返回文件的总大小 if (File.Exists(InstallFile) && string.Equals(Utility.GetMd5Hash(File.OpenRead(InstallFile)), downUnit.md5)) { //不执行下载流程 Debug.LogError("下载的文件 已经存在 : " + InstallFile); if (callback != null) { callback(totalSize, totalSize, true); } return; } if (File.Exists(tempFile)) { fs = File.OpenWrite(tempFile); startPos = fs.Length; fs.Seek(startPos, SeekOrigin.Current); //移动文件流中的当前指针 } else { string direName = Path.GetDirectoryName(tempFile); if (!Directory.Exists(direName)) { Directory.CreateDirectory(direName); } fs = new FileStream(tempFile, FileMode.Create); Debug.Log(fs.ToString()); } // 下载逻辑 HttpWebRequest request = null; WebResponse respone = null; Stream ns = null; try { request = WebRequest.Create(downUnit.downUrl) as HttpWebRequest; request.ReadWriteTimeout = ReadWriteTimeOut; request.Timeout = TimeOutWait; if (startPos > 0) { request.AddRange((int)startPos); //设置Range值,断点续传 } //向服务器请求,获得服务器回应数据流 respone = request.GetResponse(); ns = respone.GetResponseStream(); long curSize = startPos; byte[] bytes = new byte[oneReadLen]; int readSize = ns.Read(bytes, 0, oneReadLen); // 读取第一份数据 while (readSize > 0) { //如果Unity客户端关闭,停止下载 if (IsStop) { if (fs != null) { fs.Flush(); fs.Close(); fs = null; } if (ns != null) { ns.Close(); } if (respone != null) { respone.Close(); } if (request != null) { request.Abort(); } downloadThread.Stop(); } fs.Write(bytes, 0, readSize); // 将下载到的数据写入临时文件 curSize += readSize; // 回调一下 if (callback != null) { callback(curSize, totalSize, false); } // 往下继续读取 readSize = ns.Read(bytes, 0, oneReadLen); } fs.Flush(); fs.Close(); fs = null; File.Move(tempFile, InstallFile); var file = File.OpenRead(InstallFile); string md5 = Utility.GetMd5Hash(file); Debug.LogError("文件路径 :" + InstallFile + " /文件名称/ :" + downUnit.fileName + " /本地计算的md5值为/ :" + md5 + " /服务器的md5/ :" + downUnit.md5 + " /下载url / :" + downUnit.downUrl); file.Dispose(); file.Close(); if (md5 == downUnit.md5) { if (callback != null) { callback(curSize, totalSize, true); } } else { if (errorCallback != null) { errorCallback(downUnit); Debug.LogError("MD5验证失败"); } File.Delete(InstallFile); Debug.LogError("删除 删除 删除 删除 删除 删除 删除 删除 删除 删除 删除 " + InstallFile); //downloadThread.Stop(); } } catch (WebException ex) { Debug.LogError(ex); if (errorCallback != null) { errorCallback(downUnit); Debug.Log("下载出错:" + ex.Message); } } finally { if (fs != null) { fs.Flush(); fs.Close(); fs = null; } if (ns != null) { ns.Close(); } if (respone != null) { respone.Close(); } if (request != null) { request.Abort(); } } }