Esempio n. 1
0
        public static void SyncDownloadFile(string url, string savePath)
        {
            C_DebugHelper.Log("SyncDownloadFile 下载开始 url = " + url + ", savePath = " + savePath);

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                request.SendWebRequest();

                while (!request.isDone)
                {
                    if (request.isHttpError || request.isNetworkError)
                    {
                        C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                        return;
                    }
                }

                if (request.isHttpError || request.isNetworkError)
                {
                    C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                }
                else
                {
                    C_DownloadMgr.CreatFile(url, savePath, request.downloadHandler.data);
                }
            }
        }
Esempio n. 2
0
        private IEnumerator Execute(string url, string savePath, Action <byte[]> callback)
        {
            C_DebugHelper.Log("AsyncDownloadFile 下载开始 url = " + url + ", savePath = " + savePath);

            //System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            //stopWatch.Start();

            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                yield return(request.SendWebRequest());

                if (request.isHttpError || request.isNetworkError)
                {
                    C_DebugHelper.LogWarning("request.isError" + request.isNetworkError);
                }
                else
                {
                    byte[] bytes = request.downloadHandler.data;

                    if (callback != null)
                    {
                        callback(bytes);
                    }

                    C_DownloadMgr.CreatFile(url, savePath, bytes);
                }

                C_DebugHelper.Log("下载完成");

                //stopWatch.Stop();
                // C_DebugHelper.Log("下载完成,耗时: " + stopWatch.ElapsedMilliseconds);
            }
        }
Esempio n. 3
0
        IEnumerator MergeFile(string url, string savePath, Action callback)
        {
            while (true)
            {
                IsMerge = true;

                for (int i = 0; i < ThreadNum; i++)
                {
                    if (ThreadStatus[i] == false)
                    {
                        IsMerge = false;

                        yield return(0);

                        System.Threading.Thread.Sleep(100);
                        break;
                    }
                }

                if (IsMerge)
                {
                    break;
                }
            }
            byte[] bytes = new byte[bufferSize];

            FileStream fs     = new FileStream(C_DownloadMgr.StandardDownloadSavePath(url, savePath), FileMode.OpenOrCreate);
            FileStream fsTemp = null;

            for (int i = 0; i < ThreadNum; i++)
            {
                fsTemp = new FileStream(FileNames[i], FileMode.OpenOrCreate);

                int readBytes;
                while ((readBytes = fsTemp.Read(bytes, 0, bytes.Length)) > 0)
                {
                    fs.Write(bytes, 0, readBytes);
                }

                fsTemp.Close();
            }

            fs.Close();

            if (callback != null)
            {
                callback();
            }

            m_StopWatch.Stop();
            C_DebugHelper.Log("下载完成,耗时: " + m_StopWatch.ElapsedMilliseconds);

            yield return(null);

            DeleteCacheFiles();
        }
Esempio n. 4
0
        private const int m_nTimeOutWait      = 5 * 1000; //超时等待时间

        public void DownloadFile(string url, string savePath, Action callback, System.Threading.ThreadPriority threadPriority = System.Threading.ThreadPriority.Normal)
        {
            C_DebugHelper.Log("C_HttpDownloader DownloadFile url = " + url);

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

            Progress           = 0;
            DownloadFileLength = 0;
            m_bIsStop          = false;

            m_Thread = new Thread(delegate()
            {
                stopWatch.Start();

                //判断保存路径是否存在
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }

                string filePath = C_DownloadMgr.StandardDownloadSavePath(url, savePath);
                //string fileName = C_DownloadMgr.StandardDownloadName(url);

                //使用流操作文件
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);

                //获取文件现在的长度
                DownloadFileLength = fs.Length;

                //获取下载文件的总长度
                long totalLength = C_DownloadMgr.GetLength(url);
                //Debug.LogFormat("<color=red>文件:{0} 已下载{1}M,剩余{2}M</color>", fileName, fileLength / 1024 / 1024, (totalLength - fileLength) / 1024 / 1024);

                //如果没下载完
                if (DownloadFileLength < totalLength)
                {
                    //断点续传核心,设置本地文件流的起始位置
                    fs.Seek(DownloadFileLength, SeekOrigin.Begin);

                    HttpWebRequest request = C_DownloadMgr.GetWebRequest(url);

                    request.ReadWriteTimeout = m_nReadWriteTimeOut;
                    request.Timeout          = m_nTimeOutWait;

                    //断点续传核心,设置远程访问文件流的起始位置
                    request.AddRange((int)DownloadFileLength);

                    Stream stream = request.GetResponse().GetResponseStream();
                    byte[] buffer = new byte[4096];

                    //使用流读取内容到buffer中
                    //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少
                    int length = stream.Read(buffer, 0, buffer.Length);
                    while (length > 0)
                    {
                        //如果Unity客户端关闭,停止下载
                        if (m_bIsStop)
                        {
                            break;
                        }

                        //将内容再写入本地文件中
                        fs.Write(buffer, 0, length);

                        //计算进度
                        DownloadFileLength += length;
                        Progress            = (float)DownloadFileLength / (float)totalLength;

                        //类似尾递归
                        length = stream.Read(buffer, 0, buffer.Length);
                    }

                    stream.Close();
                    stream.Dispose();
                }
                else
                {
                    Progress = 1;
                }

                fs.Close();

                stopWatch.Stop();
                C_DebugHelper.Log("下载完成,耗时: " + stopWatch.ElapsedMilliseconds);

                //如果下载完毕,执行回调
                if (Progress == 1)
                {
                    if (callback != null)
                    {
                        callback();
                    }

                    m_Thread.Abort();
                }
            });

            //开启子线程
            m_Thread.IsBackground = true;
            m_Thread.Priority     = threadPriority;
            m_Thread.Start();
        }