2011-12-01 15 views
6

मैं एक TidHttp घटक का उपयोग कर वेब से बड़ी संख्या में छवियों को पुनर्प्राप्त करने का प्रयास कर रहा हूं।यूआरएल से जेपीईजी छवियों को डाउनलोड करने के लिए TidHttp का उपयोग करना (केवल वे मौजूद हैं)?

समस्या छवियों कि याद कर रहे हैं की एक संख्या है कि हो (उदाहरण: 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

आपको इसके लिए कुछ भी अतिरिक्त नहीं करना है। यदि आप एक गैर-मौजूद यूआरएल तक पहुंचने का प्रयास करते हैं, तो HTTP सर्वर एक त्रुटि रिपोर्ट करेगा जो TIdHTTPEIdHTTPProtocolException अपवाद में लपेटने से अधिक है। आपको पहले TIdHTTP.Head() पर कॉल करने से परेशान करने की आवश्यकता नहीं है, क्योंकि आप उन्हें सहेजने से पहले TMemoryStream पर छवियों को डाउनलोड कर रहे हैं। TIdHTTP.Get() पर कॉल करते समय आप अपवाद को पकड़ सकते हैं, प्रतिक्रिया कोड को बिल्कुल जांचने की आवश्यकता नहीं है।

इस प्रयास करें:

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;