从reddit下载带有java错误的图像

时间:2013-12-20 21:28:42

标签: java reddit

所以我一直在谷歌搜索如何下载图像,没有得到很好的书面解释,只是代码示例。我不完全理解这些代码,主要是

 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");

    }

}

}

EDIT 我注意到我的输出不正确,它的写作重复,我将发布我的控制台结果

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 

1 个答案:

答案 0 :(得分: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) {
            }
        }
    }
}