gpt4 book ai didi

java - 从 reddit 下载图像时出现 java 错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:17:56 26 4
gpt4 key购买 nike

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

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

有人可以像我五岁一样解释上面的代码,以及此方法的任何替代方法。

编辑2

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

最佳答案

您需要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) {
}
}
}
}

关于java - 从 reddit 下载图像时出现 java 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20712223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com