zoukankan      html  css  js  c++  java
  • 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(); 
            }
    

      

  • 相关阅读:
    String、StringBuffer与StringBuilder之间区别
    Java String之String和CharSequence、StringBuilder和StringBuffer的区别(1)
    Rational Rose2007(v7.0)下载地址、安装及激活详解教程
    FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和XmlWebApplicationContext简介
    洛克菲勒
    NOIP提高组DAY1T2——信息传递(最小环)
    洛谷P2016——战略游戏(树形)
    数字转换(树上直径)
    洛谷P2014——选课(树形dp)
    没有上司的舞会(简单树形dp)
  • 原文地址:https://www.cnblogs.com/muyeh/p/12522808.html
Copyright © 2011-2022 走看看