type THttpThread = class(TThread) private FIdHTTP: TIdHTTP; FUrl: string; FSavePath: string; procedure Down(Url: string; SavePath: string); public constructor Create(CreateSuspended: Boolean; Url: string; SavePath: string); destructor Destroy; override; procedure Execute; override; end;
{ THttpThread }
constructor THttpThread.create(CreateSuspended: Boolean; Url: string; SavePath: string); begin inherited Create(CreateSuspended); FreeOnTerminate := True; FUrl := Url; FSavePath := SavePath; FIdHTTP := TIdHTTP.Create(nil); end;
destructor THttpThread.Destroy; begin FIdHTTP .Free; inherited; end;
procedure THttpThread.Down(Url: string; SavePath: string); var Stream: TMemoryStream; begin BegTime := GetTickCount; Stream := TMemoryStream.Create; try try FIdHTTP.Get(Url, Stream); finally DeleteUrlCacheEntry(PChar(Url)); end; except MessageBox(0, '网络出错', '提示', MB_ICONQUESTION); Stream.Free; Exit; end;
Stream.SaveToFile(SavePath); Stream.Free; end;
procedure TForm1.Button2Click(Sender: TObject); var HttpThread1: THttpThread; UrlPath: string; begin UrlPath := 'http://mirror1.2ccc.com/downloads/general/multimedia/Money2ChineseCapitalaovi888.rar'; HttpThread1 := THttpThread.Create(False, UrlPath, 'C:\Test\1.Rar'); end;
procedure THttpThread.Execute; begin inherited; Down(FUrl, FSavePath); end;
|
请发表评论