2011-12-01 28 views
6

我正在尝试使用TidHttp组件从网上检索大量图像。使用TidHttp从URL下载Jpeg图像(只有那些存在的)?

的问题是,有一个数字,缺少的图像(例如:7403,7412,等等)

如何测试只有那些存在,并保存到文件?

procedure TForm.Button1Click(Sender: TObject); 
var 
    MS : TMemoryStream; 
    JPEGImage: TJPEGImage; 
    Url, numString: String; 
    I, Code: Integer; 
begin 
for I := 7400 to 7500 do 
begin 
{ 
    Url  :='http://www.mywebpage.com/images/DSC' + numString+ '.jpg'; 
    try 
    idhttp1.Head(URL); 
    code := idhttp1.ResponseCode; 
    except on E: EIdHTTPProtocolException do 
    code := idhttp1.ResponseCode; 
    end;//try except 
    if code = 200 then 
    begin 

    MS := TMemoryStream.Create; 
    JPEGImage := TJPEGImage.Create; 
    try 
    try 
    idhttp1.Get(Url, MS); //Send the request and get the image 
    code := idhttp1.ResponseCode; 
    MS.Seek(0,soFromBeginning); 
    JPEGImage.LoadFromStream(MS);//load the image in a Stream 
    Image1.Picture.Assign(JPEGImage);//Load the image in a Timage component 
    Image1.Picture.SaveToFile('C:\Museum_Data\DSC' + numString + '.jpg'); 
    Application.ProcessMessages; 
    except 
     on E: EIdHTTPProtocolException do 
     code := idhttp1.ResponseCode; // or: code := E.ErrorCode; 
    end; //try except 
      finally 
    MS.free; 
    JPEGImage.Free; 

    end; //try finally 
    end; //if 

end; 
end; 
+0

就目前为止,我可以看到你已经做好了,如果响应是200的话,但是我看不到在哪里定义了numString变量 –

回答

11

您不必为此做任何额外的事情。如果您尝试访问不存在的URL,那么HTTP服务器将报告一个错误,该错误比换行为EIdHTTPProtocolException例外。您不必先打电话TIdHTTP.Head(),因为您在保存图像之前将图像下载到TMemoryStream。当您自己调用TIdHTTP.Get()时,您可以捕获异常,根本不需要检查ResponseCode。

试试这个:

procedure TForm.Button1Click(Sender: TObject); 
var 
    MS: TMemoryStream; 
    JPEG: TJPEGImage; 
    Url: String; 
    I: Integer; 
begin 
    MS := TMemoryStream.Create; 
    try 
    JPEG := TJPEGImage.Create; 
    try 
     for I := 7400 to 7500 do 
     begin 
     Url := 'http://www.mywebpage.com/images/DSC' + IntToStr(I) + '.jpg'; 
     MS.Clear; 
     try 
      IdHTTP1.Get(Url, MS); 
     except 
      on E: EIdHTTPProtocolException do 
      Continue; 
     end; 
     MS.Position := 0; 
     JPEG.LoadFromStream(MS); 
     Image1.Picture.Assign(JPEG); 
     JPEG.SaveToFile('C:\Museum_Data\DSC' + IntToStr(I) + '.jpg'); 
     Application.ProcessMessages; 
     end; 
    finally 
     JPEG.Free; 
    end; 
    finally 
    MS.Free; 
    end; 
end; 

你实际上并不需要TImage,以将数据保存到文件中。如果你可以省略TImage.Picture.Assign()阶段,那么代码通过消除TJPEGImage干脆简单一点(除非你想验证下载文件是有效的),例如:

procedure TForm.Button1Click(Sender: TObject); 
var 
    MS: TMemoryStream; 
    Url: String; 
    I: Integer; 
begin 
    MS := TMemoryStream.Create; 
    try 
    for I := 7400 to 7500 do 
    begin 
     Url := 'http://www.mywebpage.com/images/DSC' + IntToStr(I) + '.jpg'; 
     MS.Clear; 
     try 
     IdHTTP1.Get(Url, MS); 
     except 
     on E: EIdHTTPProtocolException do 
      Continue; 
     end; 
     MS.Position := 0; 
     MS.SaveToFile('C:\Museum_Data\DSC' + IntToStr(I) + '.jpg'); 
     Application.ProcessMessages; 
    end; 
    finally 
    MS.Free; 
    end; 
end; 

或者:

procedure TForm.Button1Click(Sender: TObject); 
var 
    FS: TFileStream; 
    Url, FileName: String; 
    I: Integer; 
begin 
    for I := 7400 to 7500 do 
    begin 
    Url := 'http://www.mywebpage.com/images/DSC' + IntToStr(I) + '.jpg'; 
    FileName := 'C:\Museum_Data\DSC' + IntToStr(I) + '.jpg'; 
    FS := TFileStream.Create(FileName, fmCreate); 
    try 
     try 
     try 
      IdHTTP1.Get(Url, FS); 
     except 
      on E: EIdHTTPProtocolException do 
      Continue; 
     end; 
     Application.ProcessMessages; 
     finally 
     Fs.Free; 
     end; 
    except 
     DeleteFile(FileName); 
    end; 
    end; 
end;