2013-12-20 34 views
1

所以我一直在谷歌搜索如何下載圖像,並沒有得到很好的書面解釋,只是代碼示例。我不完全瞭解一些這個代碼,這主要與下載圖像從reddit與java錯誤

for (int b; (b = is.read()) != -1;) { 
       os.write(b); 
      } 

有人能解釋這上面的代碼像我五歲,也是任何替代這種方法。

EDIT2

import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class Downloader { 
    static String path = "C:\\reddit\\"; 

    public static void main(String[] args) { 
     connect(); 
    } 

    private static void download(String imageURL, int i) { 
     InputStream is = null; 
     OutputStream os = null; 
     try { 
      URL url = new URL(imageURL); 
      is = url.openStream(); 
      os = new FileOutputStream(path + i + ".jpg"); 
      for (int b; (b = is.read()) != -1;) { 
       os.write(b); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (is != null) { 
       try { 
        is.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      if (os != null) { 
       try { 
        os.close(); 


     } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

public static void connect() { 
    try { 
     Document doc = Jsoup.connect("http://www.reddit.com/r/pics").get(); 
     Elements url = doc.select("a"); 
     int i = 0; 
     for (Element img : url) { 

      if (img.attr("href").startsWith("http://imgur.com/")) { 
       String image = img.attr("abs:href")+".jpg"; 
       System.out.println(image); 
       i++; 
       System.out.println(i); 
       download(image, i); 
      } 
     } 
    } catch (IOException e) { 
     System.out.println("page scrape fail"); 

    } 

} 

}

編輯 我發現我的輸出是不正確的,它的寫作重複,我將只是張貼我的控制檯結果

http://imgur.com/f7rW2Of 
1 
http://imgur.com/f7rW2Of 
2 
http://imgur.com/35jpkez 
3 
http://imgur.com/35jpkez 
4 
http://imgur.com/IX9HMJG 
5 
http://imgur.com/IX9HMJG 
6 
http://imgur.com/B6MoDbT 
7 
http://imgur.com/B6MoDbT 
8 
http://imgur.com/XMtCUY9 
9 
http://imgur.com/XMtCUY9 
10 
http://imgur.com/UkbbiBl 
11 
http://imgur.com/UkbbiBl 
12 
http://imgur.com/YfLsCal 
13 
http://imgur.com/YfLsCal 
14 
http://imgur.com/9Q3CJtT 
15 
http://imgur.com/9Q3CJtT 
16 
http://imgur.com/Vt7sWTf 
17 
http://imgur.com/Vt7sWTf 
18 
http://imgur.com/hBUH5kS 
19 
http://imgur.com/hBUH5kS 
20 
http://imgur.com/gallery/OWQH0h6 
21 
http://imgur.com/gallery/OWQH0h6 
22 
http://imgur.com/a/hiJXI 
23 
http://imgur.com/a/hiJXI 
24 
+0

顯示一個實際的錯誤。是的,除了一次讀取一個字節的效率低下之外,您看起來還不錯。 –

+0

您是否因爲BufferedReader而一次讀取1個字節?我剛剛刪除了這個,我仍然沒有看到任何改變。每個文件都是29-35kb,即使我更改爲jpg,也無法查看。我再也沒有錯誤了,我可以從reddit.com/r/pics頁面下載所有27張圖片,只是無法查看其中的任何圖片。你能否真的寫作部分 – gallly

+0

他們都是JPG嗎? –

回答

0

您需要close()OutputStream osInputStream is;我會進行以下編輯 -

private static void download(String imageURL) { 
    OutputStream os = null;       // <-- Move reference here. 
    InputStream is = null; 
    try { 
     URL url = new URL(imageURL); 
     is = url.openStream(); 
     os = new BufferedOutputStream(new FileOutputStream(
       path+"1")); 
     for (int b; (b = is.read()) != -1;) { 
      os.write(b); 
     } 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (is != null) { 
      try { 
       is.close();       // <-- Call close on InputStream. 
      } catch (Exception e) { 
      } 
     } 
     if (os != null) { 
      try { 
       os.close();       // <-- Call close on OutputStream. 
      } catch (Exception e) { 
      } 
     } 
    } 
} 
+0

我做了更改,但沒有做任何更改。它仍然是一個非圖片格式 – gallly