其他图片有效,但显然不是这个。
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");
答案 0 :(得分:0)
您已经打开了与图片网址的连接,并将其存储在docker exec -it some_container bash
中。
重新使用该连接,因为这是您为其设置用户代理的连接:
httpURLCon