procedure TFmMain.BitBtn1Click(Sender: TObject); var T:TGetThread; a:TMemoryStream; savepath:string; begin Le1.Text:=Trim(Le1.Text); SavePath:=FmMain.LE2.Text; if SavePath[Length(SavePath)]<>'/' then SavePath:=SavePath+'/'; if not DirectoryExists(SavePath) then begin try if not ForceDirectories(savepath) then begin showmessage('保存路径非法'); EXIT; end; except showmessage('保存路径非法'); EXIT; end; // showmessage('保存目录不存在');
type TGetThread = class(TThread) private { Private declarations } protected IDP:TIDHTTP; procedure Execute; override; procedure GetSRC(SRC:string;S:string); Function CheckURL(URL:string):string; end;
// function Q_PosStr(const FindString, SourceString: string; StartPos: Integer): Integer;
implementation
uses UtMain,UtDownThread; { Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TGetThread.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }
{ TGetThread }
Function TGetThread.CheckURL(URL:string):string; var HURL,s,s1:string; i,a,b:integer; begin if Url[1]='.' then begin s:=copy(FmMain.LE1.Text,8,Length(FmMain.LE1.Text)-7); i:=pos('/',s); a:=pos('/',url); if i>0 then result:=copy(FmMain.LE1.Text,1,i+7)+copy(url,a+1,Length(url)-a) else result:=FmMain.le1.text+'/'+copy(url,a+1,Length(url)-a); exit; end; if Url[1]='/' then begin s:=copy(FmMain.LE1.Text,8,Length(FmMain.LE1.Text)-7); i:=pos('/',s); while i>0 do begin Delete(s,1,i); i:=pos('/',s); end; result:=copy(FmMain.LE1.Text,1,Length(FmMain.LE1.Text)-Length(s))+copy(url,2,Length(url)-1); exit; end; try HURL:=uppercase(copy(URL,1,4)); if HURL<>'HTTP' then begin s:=copy(FmMain.LE1.Text,8,Length(FmMain.LE1.Text)-7); i:=pos('/',s); if i>0 then result:=copy(FmMain.LE1.Text,1,i+7)+url else result:=FmMain.le1.text+'/'+url; end else result:=url; except result:=url; end;
end;
procedure TGetThread.GetSRC(SRC:string;S:string); var a,b:integer; PicUrl,UrlType:string; DownLoad:TDownloadPic; begin FmMain.ThreadQty:=0; a:=pos(SRC,s); while a>0 do begin delete(s,1,a+3); trimleft(s); b:=pos('>',s); if s[1]='"' then begin delete(s,1,1); b:=pos('"',s); end; if s[1]='''' then begin delete(s,1,1); b:=pos('''',s); end; PicUrl:=copy(s,1,b-1); PicUrl:=StringReplace(PicUrl,'''','',[RFReplaceAll]); PicUrl:=trim(StringReplace(PicUrl,'"','',[RFReplaceAll])); PicUrl:=CheckURl(PicURl); UrlType:=uppercase(StringReplace(copy(picurl,Length(PicUrl)-3,4),'.','',[rfReplaceAll])); if (pos('GIF',UrlType)>0) or (pos('JPG',UrlType)>0) or (pos('JPEG',UrlType)>0) or (pos('PNG',UrlType)>0) or (pos('BMP',UrlType)>0) then begin inc(FmMain.ThreadQty); DownLoad:=TDownLoadPic.Create(FmMain.ThreadQty,PicUrl); FmMain.PicCount:=FmMain.PicCount+1; FmMain.StatusBar1.Panels[0].Text:='发现 '+IntToStr(FmMain.PicCount)+' 张图片,成功下载 '+IntToStr(FmMain.DownCount)+' 张 '; Application.ProcessMessages; end; a:=pos(SRC,s); end; end;