Delphi实现类似FlashGet的多线程下载? ( 积分: 50 )

  • 主题发起人 主题发起人 formiss
  • 开始时间 开始时间
F

formiss

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi实现类似FlashGet的多线程下载?
有没有例子,或是思路.
请不啬赐教.
 
两个部分内容,多线程和下载。
多线程是你首先要知道怎么多线程,和线程之间的协调,然后是因为多线程下载,所以需要考虑下载后存入文件不同区域的问题,一般是先申请硬盘空间,建立一个用0填充的等大文件。
所以第一步先通过http请求获得文件大小,在本地建立等大文件,然后决定分几块下载,建立相应的线程,每个线程都去请求这个文件,但是请求头里面包含一个Range指定从哪里开始下载。不过服务器必须支持多线程下载
所以首先要熟悉http协议,其次是熟悉多线程。网上有很多现成的代码
 
问一下楼上的为什么我做的多线程应用程序在关闭后有时候进程中仍然会驻留呢
 
关注中~~~~~~~~~~~~~
 
{
一个多线程下载的例子
}
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, IdBaseComponent, IdAntiFreezeBase, IdAntiFreeze,
IdThreadComponent, IdFTP;

type
TThread1 = class(TThread)
private
fCount, tstart, tlast: integer;
tURL, tFile, temFileName: string;
tResume: Boolean;
tStream: TFileStream;
protected
procedure Execute;
override;
public
constructor create1(aURL, aFile, fileName: string;
bResume: Boolean;
Count,
start, last: integer);
proceduredo
wnLodeFile();
//下载文件
end;

type
TForm2 = class(TForm)
IdAntiFreeze1: TIdAntiFreeze;
IdHTTP1: TIdHTTP;
Button1: TButton;
ProgressBar1: TProgressBar;
IdThreadComponent1: TIdThreadComponent;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
Button3: TButton;
Label3: TLabel;
Label4: TLabel;
Edit1: TEdit;
Edit2: TEdit;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure IdHTTP1Workbegin
(Sender: TObject;
AWorkMode: TWorkMode;
const AWorkCountMax: Integer);
procedure IdHTTP1Work(Sender: TObject;
AWorkMode: TWorkMode;
const AWorkCount: Integer);
procedure IdHTTP1Status(ASender: TObject;
const AStatus: TIdStatus;
const AStatusText: String);
private
{ Private declarations }
public
{ Public declarations }
nn, aFileSize, avg: integer;
MyThread: array[1..10] of TThread;
procedure GetThread();
procedure AddFile();
function GetURLFileName(aURL: string): string;
function GetFileSize(aURL: string): integer;
end;

var
Form2: TForm2;
implementation
var
AbortTransfer: Boolean;
aURL, aFile: string;
tcount: integer;
//检查文件是否全部下载完毕
{$R *.dfm}
//get FileName
function TForm2.GetURLFileName(aURL: string): string;
var
i: integer;
s: string;
begin
//返回下载地址的文件名
s := aURL;
i := Pos('/', s);
while i <> 0do
//去掉&quot;/&quot;前面的内容剩下的就是文件名了
begin
Delete(s, 1, i);
i := Pos('/', s);
end;
Result := s;
end;

//get FileSize
function TForm2.GetFileSize(aURL: string): integer;
var
FileSize: integer;
begin
IdHTTP1.Head(aURL);
FileSize := IdHTTP1.Response.ContentLength;
IdHTTP1.Disconnect;
Result := FileSize;
end;

//执行下载
procedure TForm2.Button1Click(Sender: TObject);
var
j: integer;
begin
tcount := 0;
Showmessage('OK!主线程在执行,获得文件名并显示在Edit2中');
aURL := Edit1.Text;
//下载地址
aFile := GetURLFileName(Edit1.Text);
//得到文件名
nn := StrToInt(Edit2.Text);
//线程数
j := 1;
aFileSize := GetFileSize(aURL);
avg := trunc(aFileSize / nn);
begin
try
GetThread();
while j <= nndo
begin
MyThread[j].Resume;
//唤醒线程
j := j + 1;
end;
except
Showmessage('创建线程失败!');
Exit;
end;
end;
end;

//开始下载前,将ProgressBar1的最大值设置为需要接收的数据大小.
procedure TForm2.IdHTTP1Workbegin
(Sender: TObject;
AWorkMode: TWorkMode;
const AWorkCountMax: Integer);
begin
AbortTransfer := False;
ProgressBar1.Max := AWorkCountMax;
ProgressBar1.Min := 0;
ProgressBar1.Position := 0;
end;

//接收数据的时候,进度将在ProgressBar1显示出来.
procedure TForm2.IdHTTP1Work(Sender: TObject;
AWorkMode: TWorkMode;
const AWorkCount: Integer);
begin
if AbortTransfer then
begin
IdHTTP1.Disconnect;
//中断下载
end;
ProgressBar1.Position := AWorkCount;
//ProgressBar1.Position:=ProgressBar1.Position+AWorkCount;
//*******显示速度极快
Application.ProcessMessages;
//***********************************这样使用不知道对不对
end;

//中断下载
procedure TForm2.Button2Click(Sender: TObject);
begin
AbortTransfer := True;
IdHTTP1.Disconnect;
end;

//状态显示
procedure TForm2.IdHTTP1Status(ASender: TObject;
const AStatus: TIdStatus;
const AStatusText: string);
begin
ListBox1.ItemIndex := ListBox1.Items.Add(AStatusText);
end;

//退出程序
procedure TForm2.Button3Click(Sender: TObject);
begin
application.Terminate;
end;

//循环产生线程
procedure TForm2.GetThread();
var
i: integer;
start: array[1..100] of integer;
last: array[1..100] of integer;
//改用了数组,也可不用
fileName: string;
begin
i := 1;
while i <= nndo
begin
start := avg * (i - 1);
last := avg * i -1;
//这里原先是last:=avg*i;
if i = nn then
begin
last := avg*i + aFileSize-avg*nn;
//这里原先是aFileSize
end;
fileName := aFile + IntToStr(i);
MyThread := TThread1.create1(aURL, aFile, fileName, false, i, start,
last);
i := i + 1;
end;
end;

procedure TForm2.AddFile();
//合并文件
var
mStream1, mStream2: TMemoryStream;
i: integer;
begin
i := 1;
mStream1 := TMemoryStream.Create;
mStream2 := TMemoryStream.Create;
mStream1.loadfromfile(aFile + '1');
while i < nndo
begin
mStream2.loadfromfile(aFile + IntToStr(i + 1));
mStream1.seek(mStream1.size, soFrombegin
ning);
mStream1.copyfrom(mStream2, mStream2.size);
mStream2.clear;
i := i + 1;
end;
mStream2.free;
mStream1.SaveToFile(aFile);
mStream1.free;
//删除临时文件
i:=1;
while i <= nndo
begin
deletefile(aFile + IntToStr(i));
i := i + 1;
end;
Form2.ListBox1.ItemIndex := Form2.ListBox1.Items.Add('下载成功');
end;

//构造函数
constructor TThread1.create1(aURL, aFile, fileName: string;
bResume: Boolean;
Count, start, last: integer);
begin
inherited create(true);
FreeOnTerminate := true;
tURL := aURL;
tFile := aFile;
fCount := Count;
tResume := bResume;
tstart := start;
tlast := last;
temFileName := fileName;
end;
//下载文件函数
procedure TThread1.DownLodeFile();
var
temhttp: TIdHTTP;
begin

temhttp := TIdHTTP.Create(nil);
temhttp.onWorkbegin
:= Form2.IdHTTP1Workbegin
;
temhttp.onwork := Form2.IdHTTP1work;
temhttp.onStatus := Form2.IdHTTP1Status;
Form2.IdAntiFreeze1.OnlyWhenIdle := False;
//设置使程序有反应.
if FileExists(temFileName) then
//如果文件已经存在
tStream := TFileStream.Create(temFileName, fmOpenWrite)
else
tStream := TFileStream.Create(temFileName, fmCreate);
if tResume then
//续传方式
begin
exit;
end
else
//覆盖或新建方式
begin
temhttp.Request.ContentRangeStart := tstart;
temhttp.Request.ContentRangeEnd := tlast;
end;

try
temhttp.Get(tURL, tStream);
//开始下载
Form2.ListBox1.ItemIndex := Form2.ListBox1.Items.Add(temFileName +
'download');
finally
//tStream.Free;
freeandnil(tstream);
temhttp.Disconnect;
end;

end;

procedure TThread1.Execute;
begin
if Form2.Edit1.Text <> '' then
//synchronize(DownLodeFile)
do
wnLodeFile
else
exit;
inc(tcount);
if tcount = Form2.nn then
//当tcount=nn时代表全部下载成功
begin
//Showmessage('全部下载成功!');
Form2.ListBox1.ItemIndex := Form2.ListBox1.Items.Add('正在合并删除临时文件');
Form2.AddFile;
end;
end;

end.
 
后退
顶部