转自:http://www.cnblogs.com/90nice/p/3489287.html
1.因为要下载大文件 需要断点续传,使用多线程 分段下载 效率比较高,节省资源。
发点牢骚:下载可以用多线程,如果是上传 我觉得没必要了。如果是普通用户(adsl) 上传速度只有50KB/s 据我所知100MB光纤 家庭用户下载理论值能达到12.5MB/S 但是上传目前却只有 256kb/s 这点速度够干啥的。希望以后 上传下载速度能一致。
下面是一个简单的例子:
服务端:
如果是分段下载 在http Header 头文件里 要有Range ,下面会有 客户端的例子。
var range = Request.Headers["Range"]; if (null == range) { //正常情况 200 Response.StatusCode = 200; Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=" + fName); FileStream fs = new FileStream(fUrl, FileMode.Open); long len = fs.Length; fs.Close(); fs.Dispose(); Response.AddHeader("Accept-Ranges", len.ToString()); Response.TransmitFile(fUrl); } else { //Accept - Range 情况 string[] re = range.Split('='); string[] r = re[1].Split('-'); long start = 0; long alength = 0; long fslength = 0; // 如果开始为空 从0开始取 if (string.IsNullOrEmpty(r[0])) start = 0; else start = Convert.ToInt64(r[0]); //取文件总长度 FileStream fs = new FileStream(fUrl, FileMode.Open); fslength = fs.Length; fs.Close(); fs.Dispose(); // 字节长度 if (string.IsNullOrEmpty(r[1])) alength = fslength - start; else alength = Convert.ToInt64(r[1]) - start; Response.StatusCode = 206; //分段下载状态 Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=" + fName); Response.AddHeader("Accept-Ranges", (alength - start).ToString()); Response.AddHeader("Content-Range", "bytes " + start + "-" + (start + alength) + "/" + fslength.ToString()); //Content-Range: bytes 100-200/1024 告诉客户端 本次请求的文件位置和总文件大小 // 文件路径 开始位置 取多少字节 Response.TransmitFile(fUrl, start, alength);
客户端 例子:
创建一个 http请求 并 写入请求的文件段
var request = (HttpWebRequest)WebRequest.Create("http://localhost:40337/PublicPage/ZrqPublicImg.zip"); request.AddRange(0, 500); try { //获取HTTP回应,注意HttpWebResponse继承自IDisposable using (var response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) throw new Exception("文件不支持Range部分下载"); //设置接收信息的缓冲器 var bytes = new byte[5000]; //获取回应的Stream(字节流) using (var stream = response.GetResponseStream()) {//FileMode.Append 我这里是从文件末尾处写入 如果是多线程 就要根据具体的请求的位置 写入文件 using (var outStream = new FileStream(@"D:222.jpg", FileMode.Append, FileAccess.Write, FileShare.None)) { const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = stream.Read(buffer, 0, bufferLen)) > 0) { outStream.Write(buffer, 0, count); } outStream.Close(); stream.Close(); } } } } catch (Exception ex) { Console.WriteLine("错误信息:{0}", ex.Message); }
另附支持限速下载的方法(服务器代码):
/// <summary> /// 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小 /// </summary> /// <param name="_Request">;Page.Request对象</param> /// <param name="_Response">;Page.Response对象</param> /// <param name="_fileName">下载文件名</param> /// <param name="_fullPath">带文件名下载路径</param> /// <param name="_speed">每秒允许下载的字节数</param> /// <returns>返回是否成功</returns> //--------------------------------------------------------------------- //调用: // string FullPath=Server.MapPath("count.txt"); // ResponseFile(this.Request,this.Response,"count.txt",FullPath,100); //--------------------------------------------------------------------- public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed) { try { FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(myFile); try { _Response.AddHeader("Accept-Ranges", "bytes"); _Response.Buffer = false; long fileLength = myFile.Length; long startBytes = 0; int pack = 10240; //10K bytes int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1; if (_Request.Headers["Range"] != null) { _Response.StatusCode = 206; string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' }); startBytes = Convert.ToInt64(range[1]); } _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString()); if (startBytes != 0) { _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength)); } _Response.AddHeader("Connection", "Keep-Alive"); _Response.ContentType = "application/octet-stream"; _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8)); br.BaseStream.Seek(startBytes, SeekOrigin.Begin); int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1; for (int i = 0; i < maxCount; i++) { if (_Response.IsClientConnected) { _Response.BinaryWrite(br.ReadBytes(pack)); Thread.Sleep(sleep); } else { i = maxCount; } } } catch { return false; } finally { br.Close(); myFile.Close(); } } catch { return false; } return true; } }