有哪些使用chunk方法迅雷下载使用方法的网站

什么是chunked编码?_百度知道
什么是chunked编码?
chunked编码的基本方法是将大块数据分解成多块小数据,每块都可以自指定长度,其具体格式如下(BNF文法):
Chunked-Body
//0至多个chunk
last-chunk
//最后一个chunk
//尾部
//结束标记符chunk
= chunk-size [ chunk-extension ] CRLF
chunk-data CRLF
chunk-size
last-chunk
= 1*(&0&) [ chunk-extension ] CRLFchunk-extension= *( &;& chunk-ext-name [ &=& chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val
= token | quoted-string
chunk-data
= chunk-size(OCTET)
= *(entity-header CRLF)
Chunked-Body表示经过chunked编码后的报文体。报文体可以分为chunk, last-chunk,trailer和结束符四部分。chunk的数量在报文体中最少可以为0,无上限;每个chunk的长度是自指定的,即,起始的数据必然是16进制数字的字符串,代表后面chunk-data的长度(字节数)。这个16进制的字符串第一个字符如果是“0”,则表示chunk-size为0,该chunk为last-chunk,无chunk-data部分。可选的chunk-extension由通信双方自行确定,如果接收者不理解它的意义,可以忽略。
trailer是附加的在尾部的额外头域,通常包含一些元数据(metadata, meta means &about information&),这些头域可以在解码后附加在现有头域之后。
实例分析:
下面分析用ethereal抓包使用Firefox与某网站通信的结果(从头域结束符后开始):Address
0..........................
66 66 63 0d 0a ...............
// ASCII码:1ffc\r\n, chunk-data数据起始地址为000d5
很明显,“1ffc”为第一个chunk的chunk-size,转换为int为8188.由于1ffc后马上就是
CRLF,因此没有chunk-extension.chunk-data的起始地址为000d5, 计算可知下一块chunk的起始
地址为000d5+1ffc + 2=020d3,如下:020d0
.. 0d 0a 31 66 66 63 0d 0a .... // ASCII码:\r\n1ffc\r\n
前一个0d0a是上一个chunk的结束标记符,后一个0d0a则是chunk-size和chunk-data的分隔符。
此块chunk的长度同样为8188, 依次类推,直到最后一块100e0
0d 0a 31100f0
65 61 39 0d 0a......
//ASII码:\r\n\1ea9\r\n
此块长度为0x1ea9 = 7849, 下一块起始为100f5 + 1ea9 + 2 = 11fa0,如下:100a0
30 0d 0a 0d 0a
//ASCII码:0\r\n\r\n
“0”说明当前chunk为last-chunk, 第一个0d 0a为chunk结束符。第二个0d0a说明没有trailer部分,整个Chunk-body结束。
解码流程:
对chunked编码进行解码的目的是将分块的chunk-data整合恢复成一块作为报文体,同时记录此块体的长度。
RFC2616中附带的解码流程如下:(伪代码)
length := 0
//长度计数器置0
read chunk-size, chunk-extension (if any) and CRLF
//读取chunk-size, chunk-extension
//和CRLF
while(chunk-size & 0 )
//表明不是last-chunk
read chunk-data and CRLF
//读chunk-size大小的chunk-data,skip CRLF
append chunk-data to entity-body
//将此块chunk-data追加到entity-body后
read chunk-size and CRLF
//读取新chunk的chunk-size 和 CRLF
read entity-header
//entity-header的格式为name:valueCRLF,如果为空即只有CRLF
while (entity-header not empty)
//即,不是只有CRLF的空行
append entity-header to existing header fields
read entity-header
Content-Length:=length
//将整个解码流程结束后计算得到的新报文体length
//作为Content-Length域的值写入报文中
Remove &chunked& from Transfer-Encoding
//同时从Transfer-Encoding中域值去除chunked这个标记
length最后的值实际为所有chunk的chunk-size之和,在上面的抓包实例中,一共有八块chunk-size为0x1ffc(8188)的chunk,剩下一块为0x1ea9(7849),加起来一共73353字节。
注:对于上面例子中前几个chunk的大小都是8188,可能是因为:&1ffc& 4字节,&\r\n&2字节,加上块尾一个&\r\n&2字节一共8字节,因此一个chunk整体为8196,正好可能是发送端一次TCP发送的缓存大小。
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁protected void Button1_Click(object sender, EventArgs e)& {& /*& 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite& 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。& 代码如下:& */& Response.ContentType = "application/x-zip-compressed";& Response.AddHeader("Content-Disposition", "filename=z.zip");& string filename = Server.MapPath("DownLoad/aaa.zip");& Response.TransmitFile(filename);& }& //WriteFile实现下载& protected void Button2_Click(object sender, EventArgs e)& {& /*& using System.IO;& & & */& string fileName ="aaa.zip";//客户端保存的文件名& string filePath=Server.MapPath("DownLoad/aaa.zip");//路径& FileInfo fileInfo = new FileInfo(filePath);& Response.Clear();& Response.ClearContent();& Response.ClearHeaders();& Response.AddHeader("Content-Disposition", "filename=" + fileName);& Response.AddHeader("Content-Length", fileInfo.Length.ToString());& Response.AddHeader("Content-Transfer-Encoding", "binary");& Response.ContentType = "application/octet-stream";& Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");& Response.WriteFile(fileInfo.FullName);& Response.Flush();& Response.End();& }& //WriteFile分块下载& protected void Button3_Click(object sender, EventArgs e)& {& string fileName = "aaa.zip";//客户端保存的文件名& string filePath = Server.MapPath("DownLoad/aaa.zip");//路径& System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);& if (fileInfo.Exists == true)& {& const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力& byte[] buffer = new byte[ChunkSize];& Response.Clear();& System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);& long dataLengthToRead = iStream.L//获取下载的文件总大小& Response.ContentType = "application/octet-stream";& Response.AddHeader("Content-Disposition", " filename=" + HttpUtility.UrlEncode(fileName));& while (dataLengthToRead & 0 && Response.IsClientConnected)& & {& int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小& Response.OutputStream.Write(buffer, 0, lengthRead);& Response.Flush();& dataLengthToRead = dataLengthToRead - lengthR& }& Response.Close();& }& }& //流方式下载& protected void Button4_Click(object sender, EventArgs e)& {& string fileName = "aaa.zip";//客户端保存的文件名& string filePath = Server.MapPath("DownLoad/aaa.zip");//路径& //以字符流的形式下载文件& FileStream fs = new FileStream(filePath, FileMode.Open);& byte[] bytes = new byte[(int)fs.Length];& fs.Read(bytes, 0, bytes.Length);& fs.Close();& Response.ContentType = "application/octet-stream";& //通知浏览器下载文件而不是打开& Response.AddHeader("Content-Disposition", " filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));& Response.BinaryWrite(bytes);& Response.Flush();& Response.End();& }&
阅读(...) 评论()&&&&&&正文
ASP.NET下载文件几种方式
摘要:protected void Button1_Click(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e) & { &/* & 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite & 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 & 代码如下: & */ &Response.ContentType = &application/x-zip-compressed&; & &Response.AddHeader(&Content-Disposition&, &filename=z.zip&); & &string filename = Server.MapPath(&DownLoad/aaa.zip&); & &Response.TransmitFile(filename); & &} &//WriteFile实现下载 &protected void Button2_Click(object sender, EventArgs e) & { &/* & using System.IO; & & & */ &string fileName =&aaa.zip&;//客户端保存的文件名 &string filePath=Server.MapPath(&DownLoad/aaa.zip&);//路径 &FileInfo fileInfo = new FileInfo(filePath); &Response.Clear(); &Response.ClearContent(); &Response.ClearHeaders(); &Response.AddHeader(&Content-Disposition&, &filename=& + fileName); &Response.AddHeader(&Content-Length&, fileInfo.Length.ToString()); &Response.AddHeader(&Content-Transfer-Encoding&, &binary&); &Response.ContentType = &application/octet-stream&; &Response.ContentEncoding = System.Text.Encoding.GetEncoding(&gb2312&); &Response.WriteFile(fileInfo.FullName); & Response.Flush(); & Response.End(); &} &//WriteFile分块下载 &protected void Button3_Click(object sender, EventArgs e) & { & &string fileName = &aaa.zip&;//客户端保存的文件名 & & &string filePath = Server.MapPath(&DownLoad/aaa.zip&);//路径 & &System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); & &if (fileInfo.Exists == true) & & { & & & & &const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 & & & & &byte[] buffer = new byte[ChunkSize]; & & & &Response.Clear(); & & & & &System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); & & & & &long dataLengthToRead = iStream.L//获取下载的文件总大小 & & & & &Response.ContentType = &application/octet-stream&; & & & &Response.AddHeader(&Content-Disposition&, & filename=& + HttpUtility.UrlEncode(fileName)); & &while (dataLengthToRead & 0 && Response.IsClientConnected)
& &{ & & & & &int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 & & & & &Response.OutputStream.Write(buffer, 0, lengthRead); & Response.Flush(); & & & &dataLengthToRead = dataLengthToRead - lengthR & &} & & & & &Response.Close(); & & &} &} &//流方式下载 &protected void Button4_Click(object sender, EventArgs e) &{ & & & & string fileName = &aaa.zip&;//客户端保存的文件名 & & & string filePath = Server.MapPath(&DownLoad/aaa.zip&);//路径 & & & //以字符流的形式下载文件 & & & &FileStream fs = new FileStream(filePath, FileMode.Open); & & & &byte[] bytes = new byte[(int)fs.Length]; & & & &fs.Read(bytes, 0, bytes.Length); & & & &fs.Close(); & & & &Response.ContentType = &application/octet-stream&; & //通知浏览器下载文件而不是打开 & & & Response.AddHeader(&Content-Disposition&, & filename=& + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); & & Response.BinaryWrite(bytes); & & & Response.Flush(); & & & Response.End(); &}
全国校区查询
技术分享点击榜
技术分享最新文章
官方新版意见收集
*您的积极反馈是我们前进的动力
官方新版意见收集
提交成功,感谢您的反馈。
我们会认真阅读和考虑每个用户的反馈。}

我要回帖

更多关于 冰点下载使用方法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信