F
firebirdrui
Unregistered / Unconfirmed
GUEST, unregistred user!
网上关于http多线程下载都提出把文件平均分配给每个线程,可惜我照做之后,合并起来的文件总是跟原文件大小不一样.哪位高手指点一下,高分回报.小弟新手.对delphi一点不懂.
//循环产生线程
procedure TForm1.GetThread();
var
i ,start,last : integer;
fileName : String;
begin
i:=1;
while i<=nndo
//nn为设定好的线程数.
begin
if i=nn then
begin
start := avg*(i-1);
last := aFileSize;//aFileSize为服务器上文件的大小
end
else
start := avg*(i-1);
last := avg*i;
fileName:=aFile+IntToStr(i);//fileName为该线程下载的那一部分临时文件名
MyThread:=TThread1.create1(aURL,fileName, false ,start,last);
i :=i+1;
end;
end;
//下载文件函数
procedure TThread1.DownLodeFile();
var
temhttp: TIdHTTP;
begin
temhttp := TIdHTTP.Create(nil);
Form1.IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
if FileExists(temFileName) then
//如果文件已经存在
tStream := TFileStream.Create(temFileName, fmOpenWrite) else
tStream := TFileStream.Create(temFileName, fmCreate);
temhttp.Request.ContentRangeStart := tstart;
temhttp.Request.ContentRangeEnd := tlast;
try
temhttp.Get(tURL, tStream);
//开始下载
Form1.ListBox1.ItemIndex := Form1.ListBox1.Items.Add(temFileName+'download');
finally
tStream.Free;
end;
end;
而合并只是反复用了这两句话.
mStream1.seek(mStream1.size,soFrombegin
ning);
mStream1.copyfrom(mStream2,mStream2.size);
哪位大哥对文件比较精通指点下小弟...
//循环产生线程
procedure TForm1.GetThread();
var
i ,start,last : integer;
fileName : String;
begin
i:=1;
while i<=nndo
//nn为设定好的线程数.
begin
if i=nn then
begin
start := avg*(i-1);
last := aFileSize;//aFileSize为服务器上文件的大小
end
else
start := avg*(i-1);
last := avg*i;
fileName:=aFile+IntToStr(i);//fileName为该线程下载的那一部分临时文件名
MyThread:=TThread1.create1(aURL,fileName, false ,start,last);
i :=i+1;
end;
end;
//下载文件函数
procedure TThread1.DownLodeFile();
var
temhttp: TIdHTTP;
begin
temhttp := TIdHTTP.Create(nil);
Form1.IdAntiFreeze1.OnlyWhenIdle:=False;//设置使程序有反应.
if FileExists(temFileName) then
//如果文件已经存在
tStream := TFileStream.Create(temFileName, fmOpenWrite) else
tStream := TFileStream.Create(temFileName, fmCreate);
temhttp.Request.ContentRangeStart := tstart;
temhttp.Request.ContentRangeEnd := tlast;
try
temhttp.Get(tURL, tStream);
//开始下载
Form1.ListBox1.ItemIndex := Form1.ListBox1.Items.Add(temFileName+'download');
finally
tStream.Free;
end;
end;
而合并只是反复用了这两句话.
mStream1.seek(mStream1.size,soFrombegin
ning);
mStream1.copyfrom(mStream2,mStream2.size);
哪位大哥对文件比较精通指点下小弟...