• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#分块下载文件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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(); 
        }

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#读取Ini配置文件类发布时间:2022-07-14
下一篇:
C#一个简单的递归函数和两个ListT合并发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap