System.Net.HttpWebRequest request =(System.Net.HttpWebRequestSystem.Net.HttpWebRequest.Create(s_url);request.AddRange((int)(起始位置),(int)终止位置); //设置Range值;以上代码可以从文件不同的地方下载,但是:
1.从多个地方下载时,下载到本地的的文件是怎么组合的?
public System.IO.FileStream fs;
fs.Seek(起始位置,System.IO.SeekOrigin.Current ); //移动文件流中的当前指针
上述方法也不行,有没有更好的方法?
2.如果要记录每个进程下载多少的话如何记录呢?
1.从多个地方下载时,下载到本地的的文件是怎么组合的?
public System.IO.FileStream fs;
fs.Seek(起始位置,System.IO.SeekOrigin.Current ); //移动文件流中的当前指针
上述方法也不行,有没有更好的方法?
2.如果要记录每个进程下载多少的话如何记录呢?
然后打开文件流(不管多少线程都只有一个),这个时候fs.Seek类似的方法就能使用了问题二:
每个下载线程有自己的缓冲区
像flashget这样的软件,下载文件之前会先在磁盘分配空间,这个操作其实就是,往文件注入相同长度的空白数据
然后打开文件流(不管多少线程都只有一个),这个时候fs.Seek类似的方法就能使用了问题二:
每个下载线程有自己的缓冲区-----------------------------------------
能不能具体点?下面是我的代码:
public class DownThread
{
public System.IO.FileStream fs;
public string s_url;
public long start_pos,end_pos;
public int block,row_no;
public ListView lv; public DownThread(System.IO.FileStream fs,string s_url, long start_pos,long end_pos,int block,ListView lv,int row_no)
{
this.fs = fs;//
this.s_url = s_url; //URL
this.start_pos = start_pos;//起始位置
this.end_pos = end_pos; //终止位置
}
public void ThreadProc()
{
try
{
long have_down = 0;
int th_down_col = 0;
System.Net.HttpWebRequest request =(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(s_url);
request.AddRange((int)(start_pos),(int)end_pos); //设置Range值;
System.IO.Stream ns= request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[1024*32];
int nReadSize=0;
nReadSize=ns.Read(nbytes,0,1024*32);
fs.Seek(start_pos,System.IO.SeekOrigin.Current ); //移动文件流中的当前指针
//fs.Position = start_pos;
while( nReadSize >0)
{
fs.Write(nbytes,0,nReadSize);
nReadSize=ns.Read(nbytes,0,1024*32);
have_down = have_down + nReadSize;
}
}
catch{}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
...
... fs = new System.IO.FileStream(FilePath,System.IO.FileMode.Create); ...
... DownThread DownThread1 = new DownThread(fs,StrUrl, 1,1024);
Thread th1 = new Thread(new ThreadStart(DownThread1.ThreadProc ));
th1.Start(); DownThread DownThread2 = new DownThread(fs,StrUrl, 1,1024);
Thread th2 = new Thread(new ThreadStart(DownThread2.ThreadProc ));
th2.Start(); ...
...
}
以上是主要代码,跟主题无关的代码没有贴出来。请问如何改才能实现这样的功能?
像flashget这样的软件,下载文件之前会先在磁盘分配空间,这个操作其实就是,往文件注入相同长度的空白数据
然后打开文件流(不管多少线程都只有一个),这个时候fs.Seek类似的方法就能使用了问题二:
每个下载线程有自己的缓冲区-----------------------------------------
能不能具体点?下面是我的代码:
public class DownThread
{
public System.IO.FileStream fs;
public string s_url;
public long start_pos,end_pos;
public int block,row_no;
public ListView lv; public DownThread(System.IO.FileStream fs,string s_url, long start_pos,long end_pos,int block,ListView lv,int row_no)
{
this.fs = fs;//
this.s_url = s_url; //URL
this.start_pos = start_pos;//起始位置
this.end_pos = end_pos; //终止位置
}
public void ThreadProc()
{
try
{
long have_down = 0;
int th_down_col = 0;
System.Net.HttpWebRequest request =(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(s_url);
request.AddRange((int)(start_pos),(int)end_pos); //设置Range值;
System.IO.Stream ns= request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[1024*32];
int nReadSize=0;
nReadSize=ns.Read(nbytes,0,1024*32);
fs.Seek(start_pos,System.IO.SeekOrigin.Current ); //移动文件流中的当前指针
//fs.Position = start_pos;
while( nReadSize >0)
{
fs.Write(nbytes,0,nReadSize);
nReadSize=ns.Read(nbytes,0,1024*32);
have_down = have_down + nReadSize;
}
}
catch{}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
...
... fs = new System.IO.FileStream(FilePath,System.IO.FileMode.Create); ...
... DownThread DownThread1 = new DownThread(fs,StrUrl, 1,1024);
Thread th1 = new Thread(new ThreadStart(DownThread1.ThreadProc ));
th1.Start(); DownThread DownThread2 = new DownThread(fs,StrUrl, 1,1024);
Thread th2 = new Thread(new ThreadStart(DownThread2.ThreadProc ));
th2.Start(); ...
...
}
以上是主要代码,跟主题无关的代码没有贴出来。请问如何改才能实现这样的功能?
你的代码有点问题,Close少了好几个哦!你的IIS很快就会完蛋的。
以下是一个请求报文与相应的回复报文的例子:
GET /image/index_r4_c1.jpg HTTP/1.1Accept: */*Referer: http://192.168.3.120:8080Accept-Language: zh-cnAccept-Encoding: gzip, deflateUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)Host: 192.168.3.120:8080Connection: Keep-AliveHTTP/1.1 200 OKServer: Microsoft-IIS/5.0Date: Tue, 24 Jun 2003 05:39:40 GMTContent-Type: image/jpegAccept-Ranges: bytesLast-Modified: Thu, 23 May 2002 03:05:40 GMTETag: "bec48eb862c21:934"Content-Length: 2827? JFIF H H C [1]….下面我们就来说说“断点续传”。
顾名思义,断点续传就是在上一次下载时断开的位置开始继续下载。在HTTP协议中,可以在请求报文头中加入Range段,来表示客户机希望从何处继续下载。
比如说从第1024字节开始下载,请求报文如下:
GET /image/index_r4_c1.jpg HTTP/1.1Accept: */*Referer: http://192.168.3.120:8080Accept-Language: zh-cnAccept-Encoding: gzip, deflateUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)Host: 192.168.3.120:8080Range:bytes=1024-Connection: Keep-Alive.NET中的相关类明白了上面的原理,那么,我们来看看.NET FRAMEWORK中为我们提供了哪些类可以来做这些事。
完成HTTP请求System.Net.HttpWebRequestHttpWebRequest 类对 WebRequest 中定义的属性和方法提供支持,也对使用户能够直接与使用 HTTP 的服务器交互的附加属性和方法提供支持。
HttpWebRequest 将发送到 Internet 资源的公共 HTTP 标头值公开为属性,由方法或系统设置。下表包含完整列表。可以将 Headers 属性中的其他标头设置为名称/值对。但是注意,某些公共标头被视为受限制的,它们或者直接由 API公开,或者受到系统保护,不能被更改。Range也属于被保护之列,不过,.NET为开发者提供了更方便的操作,就是 AddRange方法,向请求添加从请求数据的开始处或结束处的特定范围的字节范围标头完成文件访问System.IO.FileStreamFileStream 对象支持使用Seek方法对文件进行随机访问, Seek 允许将读取/写入位置移动到文件中的任意位置。这是通过字节偏移参考点参数完成的。字节偏移量是相对于查找参考点而言的,该参考点可以是基础文件的开始、当前位置或结尾,分别由SeekOrigin类的三个属性表示。
代码实现了解了.NET提供的相关的类,那么,我们就可以方便的实现了。
代码如下:
static void Main(string[] args)
{
string StrFileName="c:\\aa.zip"; //根据实际情况设置
string StrUrl="http://www.xxxx.cn/xxxxx.zip"; //根据实际情况设置
//打开上次下载的文件或新建文件
long lStartPos =0;
System.IO.FileStream fs;
if (System.IO.File.Exists(StrFileName))
{
fs= System.IO.File.OpenWrite(StrFileName);
lStartPos=fs.Length;
fs.Seek(lStartPos,System.IO.SeekOrigin.Current); //移动文件流中的当前指针
}
else
{
fs = new System.IO.FileStream(StrFileName,System.IO.FileMode.Create);
lStartPos =0;
}
//打开网络连接
try
{
System.Net.HttpWebRequest request =(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
if ( lStartPos>0)
request.AddRange((int)lStartPos); //设置Range值
//向服务器请求,获得服务器回应数据流
System.IO.Stream ns= request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[512];
int nReadSize=0;
nReadSize=ns.Read(nbytes,0,512);
while( nReadSize >0)
{
fs.Write(nbytes,0,nReadSize);
nReadSize=ns.Read(nbytes,0,512);
}
fs.Close();
ns.Close();
Console.WriteLine("下载完成");
}
catch(Exception ex)
{
fs.Close();
Console.WriteLine("下载过程中出现错误:"+ex.ToString());
}
}
用一个静态文件(公用)下载完就好了,拼什么
------------------------------
这样更好了,怎么用?
2)请问这里的高手,用SOCKET如何实现断点续传呢?
{string StrFileName="c:\\aa.zip"; //根据实际情况设置
string StrUrl="http://www.xxxx.cn/xxxxx.zip"; //根据实际情况设置//打开上次下载的文件或新建文件
long lStartPos =0;
System.IO.FileStream fs;
if (System.IO.File.Exists(StrFileName))
{
fs= System.IO.File.OpenWrite(StrFileName);
lStartPos=fs.Length;
fs.Seek(lStartPos,System.IO.SeekOrigin.Current); //移动文件流中的当前指针
}
else
{
fs = new System.IO.FileStream(StrFileName,System.IO.FileMode.Create);
lStartPos =0;
}//打开网络连接
try
{
System.Net.HttpWebRequest request =(System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
if ( lStartPos>0)
request.AddRange((int)lStartPos); //设置Range值//向服务器请求,获得服务器回应数据流
System.IO.Stream ns= request.GetResponse().GetResponseStream();byte[] nbytes = new byte[512];
int nReadSize=0;
nReadSize=ns.Read(nbytes,0,512);
while( nReadSize >0)
{
fs.Write(nbytes,0,nReadSize);
nReadSize=ns.Read(nbytes,0,512);
}
fs.Close();
ns.Close();
Console.WriteLine("下载完成");
}
catch(Exception ex)
{
fs.Close();
Console.WriteLine("下载过程中出现错误:"+ex.ToString());
}
}