需求:从阿里云oss下载大量的图片(4000+)到手机的内部存储
/// <summary>/// 指定要下载的文件列表/// </summary>/// <param name="aliyunFile">阿里云上要下载文件所在文件夹</param>/// <param name="saveFile">下载文件存储文件夹(Application.streamingAssetsPath下)</param>/// <param name="completedCallback">下载完成回调事件</param>/// <param name="needUI">下载过程中是否需要展示UI</param>public void StartDownloadFileList(List<string> downloadFileList, string saveFile, Action completedCallback = null, bool needUI = true){if (NetworkStatus.IsNetNotReachable()){TipsManager.Instance.ShowTip("当前设备网络未连接,请检查网络");return;}Debug.Log(TAG + "StartDownloadFileList by list");if (needUI){StatusText.text = "资源下载中...";}curDownloadIndex = lastDownloadIndex = 0;if (completedCallback != null){DownloadCompletedCallback = completedCallback;}ShowDownloadUI = needUI;allFileList = downloadFileList;if (allFileList.Count > 0){DownloadSingleObj(saveFile, allFileList[curDownloadIndex]);}}/// <summary>/// 下载单个文件/// </summary>/// <param name="targetFile">本地存储路径文件夹</param>/// <param name="filePath">oss上文件存储路径</param>/// <param name="savePath">直接指定的存储路径</param>public void DownloadSingleObj(string targetFile, string filePath, string savePath = ""){Debug.Log("下载单个文件: " + filePath);string dir = StringTranslation.GetStoragePath() + "/" + targetFile + "/" + filePath.Substring(0, filePath.LastIndexOf("/"));thread = new Thread(GetObject);if (string.IsNullOrEmpty(savePath)){FileHelper.CreateDirectory(dir);GetObjectByThread(targetFile, filePath, StringTranslation.GetStoragePath() + "/" + targetFile + "/" + filePath);}else{GetObjectByThread(targetFile, filePath, savePath);}}private void GetObjectByThread(string targetFile, string filePath, string savePath){this.targetFile = targetFile;this.filePath = filePath;this.savePath = savePath;//thread = new Thread(GetObject);thread.Start();}private void GetObject(){try{GetObjectRequest getObjectRequest = new GetObjectRequest(AliyunConfig.BucketName, filePath);getObjectRequest.StreamTransferProgress += StreamProcess;OssObject result = client.GetObject(getObjectRequest);using (var resultStream = result.Content){string directory = savePath.Substring(0, savePath.LastIndexOf("/"));FileHelper.CreateDirectory(directory);using (var fs = File.Open(savePath, FileMode.OpenOrCreate)){int length = (int)resultStream.Length;byte[] bytes = new byte[length];do{length = resultStream.Read(bytes, 0, length);fs.Write(bytes, 0, length);} while (length != 0);}print(string.Format("第{0}个下载成功 。", curDownloadIndex + 1));if (curDownloadIndex < allFileList.Count - 1){{curDownloadIndex++;}DownloadSingleObj(targetFile, allFileList[curDownloadIndex]);}else{Loom.QueueOnMainThread(() =>{print("所有文件下载完毕 " + (curDownloadIndex + 1));if (ShowDownloadUI){StatusText.text = "资源下载完毕";}DownloadCompletedCallback?.Invoke();DownloadCompletedCallback = null;CancelInvoke("CheckDownloadProcess");});thread.Abort();}}}catch (OssException e){print("进度下载文件出错:" + e.Message);}}
问题:下载一部分之后,下载进度会停止;
解决办法: 检测下载进度是否停止,如果停止,则重启线程继续下载;
IEnumerator RestartThread(){thread.Abort();yield return new WaitForSeconds(Time.deltaTime);thread = new Thread(GetObject);thread.Start();DownloadSingleObj(targetFile, allFileList[curDownloadIndex]);}InvokeRepeating("CheckDownloadProcess", 0, 3);