Exemple #1
0
        /// <summary>
        /// 取消下载
        /// </summary>
        public void CloseDown()
        {
            if (td != null)
            {
                td.Abort();
            }
            if (fs != null)
            {
                fs.Close();
            }
            if (ns != null)
            {
                ns.Close();
            }
            Down_tm   = new System.Windows.Forms.Timer();
            autoEvent = new AutoResetEvent(false);
            string StrFileName = DownPath + name; //根据实际情况设置

            CommonsCall.DeletePath(StrFileName);
        }
Exemple #2
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="localFilePath"></param>
        /// <param name="url"></param>
        public int DownFile(string url, int threadCount)
        {
            autoEvent.WaitOne();  //阻塞当前线程,等待通知以继续执行
            //await LoadAsync(url,threadCount);


            string StrFileName = DownPath + name; //根据实际情况设置
            string StrUrl      = url;             //根据实际情况设置
            //打开上次下载的文件或新建文件
            long lStartPos = 0;

            if (System.IO.File.Exists(StrFileName))//另外如果文件已经下载完毕,就不需要再断点续传了,不然请求的range 会不合法会抛出异常。
            {
                fs        = System.IO.File.OpenWrite(StrFileName);
                lStartPos = fs.Length;
                fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针
            }
            else
            {
                fs        = new FileStream(StrFileName, System.IO.FileMode.Create);
                lStartPos = 0;
            }
            //打开网络连接
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
                if (lStartPos > 0)
                {
                    request.AddRange((int)lStartPos); //设置Range值
                }
                request.Timeout = 20000;
                System.Net.WebResponse response = request.GetResponse();
                //向服务器请求,获得服务器回应数据流
                ns = response.GetResponseStream();
                long   totalSize   = response.ContentLength;
                long   hasDownSize = 0;
                byte[] nbytes      = new byte[1024 * 2];//521,2048 etc
                int    nReadSize   = 0;
                nReadSize = ns.Read(nbytes, 0, nbytes.Length);
                while (nReadSize > 0)
                {
                    fs.Write(nbytes, 0, nReadSize);
                    downCount++;
                    nReadSize    = ns.Read(nbytes, 0, 1024 * 2);
                    hasDownSize += nReadSize;
                }
                fs.Close();
                ns.Close();
                response.Dispose();
                //CommonsCall.Compress(downStuep, StrFileName);
                CommonsCall.DeletePath(StrFileName);
                GC.Collect();
            }
            catch (ThreadAbortException e)
            {
                Thread.Sleep(10000);
                DownFile(url, threadCount);
            }
            catch (Exception ex)
            {
                Thread.Sleep(10000);
                DownFile(url, threadCount);
            }
            finally
            {
                threadCount--;
            }
            return(threadCount);
        }