C#分块下载文件

1.分块下载文件代码

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url">文件url</param>
        /// <param name="docName"></param>
        /// <returns></returns> 
        public string DownFileByUrl(string url,string docName)
        { 
            //url 如: http://www.xxx.com:888/UpFiles/1111.txt
            string serverpath = System.AppDomain.CurrentDomain.BaseDirectory + "UpFiles\BPMDown";
            if (!Directory.Exists(serverpath))
                Directory.CreateDirectory(serverpath);
            string filePath = serverpath + "\" + docName;
             
            if (File.Exists(filePath))
            {//如果已经存在,则删除
                try
                {
                    File.Delete(filePath);
                }
                catch (Exception)
                { 
                } 
            }
           
            using (FileStream fss = new FileStream(filePath, FileMode.Create))
            {
                HttpTrunkDownLoad(fss, url);
            }
           //HttpDownLoad(filePath, url, docName);

            return "\UpFiles\Down\" + docName; 
        }
        /// <summary>
        /// 分块下载文件
        /// </summary>
        /// <param name="fs"></param>
        /// <param name="reqUrl"></param> 
        public void HttpTrunkDownLoad(FileStream fs ,string reqUrl )
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqUrl);
                request.Method = "GET";
                request.ProtocolVersion = new Version(1, 1);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    throw new Exception("该文件在BPM服务器上不存在!");//找不到 
                }
                // 获取返回的数据
                Stream res = response.GetResponseStream();
                long length = response.ContentLength;

                long readCnt = 0;
                long readLength = 0;
                int trunksize = 5 * 1024 * 1024;    //分块下载,每块的大小
                while (readCnt != length)
                {
                    byte[] bytes = new byte[trunksize];
                    readLength = res.Read(bytes, 0, trunksize);
                    readCnt = readCnt + readLength;
                    fs.Write(bytes, 0, (int)readLength);
                } 
            }
            catch (Exception ex)
            {
                fs.Close();
                fs.Dispose();
                throw ex; 
            } 
        }

  2.不块下载

     //下载文件 
        public void HttpDownLoad(string filePath,string url,string docName)
        { 
            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ProtocolVersion = new Version(1, 1);
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new Exception("该文件在BPM服务器上不存在!");///找不到则直接返回null
            }
            // 转换为byte类型
            System.IO.Stream stream = response.GetResponseStream();
            //创建本地文件写入流
            Stream fs = new FileStream(filePath, FileMode.Create);
            byte[] bArr = new byte[1024];
            int size = stream.Read(bArr, 0, (int)bArr.Length);
            while (size > 0)
            {
                fs.Write(bArr, 0, size);
                size = stream.Read(bArr, 0, (int)bArr.Length);
            }
            fs.Close();
            stream.Close(); 
        }

  

原文地址:https://www.cnblogs.com/muyeh/p/12522808.html