Java |存储来自 URL 的图像 | java.io.IOException:URL 403
Java | storing image from URL | java.io.IOException: 403 for URL
其他图像有效,但显然不是这个。
https://www.kingjamesbibleonline.org/Inspirational-Images/large/Isaiah_55-6.jpg
代码:
public static void imageFromURL(URL url, String saveAs) throws IOException {
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
httpURLCon.addRequestProperty("User-Agent", "Mozilla/4.76");
BufferedImage c = ImageIO.read(url.openStream());
File outputFile = new File("C:.../resources/" + saveAs);
ImageIO.write(c, "jpg", outputFile);
}
错误信息:
java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.kingjamesbibleonline.org/Inspirational-Images/large/Isaiah_55-6.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at MainActivity.imageFromURL(MainActivity.java:41)
at MainActivity.main(MainActivity.java:49)
尝试过其他解决方案,例如:
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
uc = url.openConnection();
uc.addRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
您已经打开了与图像 URL 的连接并将其存储在 httpURLCon
中。
重新使用该连接,因为这是您为其设置了用户代理的连接:
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
httpURLCon.addRequestProperty("User-Agent", "Mozilla/4.76");
BufferedImage c = ImageIO.read(httpURLCon.getInputStream());
其他图像有效,但显然不是这个。
https://www.kingjamesbibleonline.org/Inspirational-Images/large/Isaiah_55-6.jpg
代码:
public static void imageFromURL(URL url, String saveAs) throws IOException {
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
httpURLCon.addRequestProperty("User-Agent", "Mozilla/4.76");
BufferedImage c = ImageIO.read(url.openStream());
File outputFile = new File("C:.../resources/" + saveAs);
ImageIO.write(c, "jpg", outputFile);
}
错误信息:
java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.kingjamesbibleonline.org/Inspirational-Images/large/Isaiah_55-6.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at MainActivity.imageFromURL(MainActivity.java:41)
at MainActivity.main(MainActivity.java:49)
尝试过其他解决方案,例如:
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
uc = url.openConnection();
uc.addRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
您已经打开了与图像 URL 的连接并将其存储在 httpURLCon
中。
重新使用该连接,因为这是您为其设置了用户代理的连接:
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
httpURLCon.addRequestProperty("User-Agent", "Mozilla/4.76");
BufferedImage c = ImageIO.read(httpURLCon.getInputStream());