我有以下代码:
public BufferedImage urlToImage(String imageUrl) throws MalformedURLException, IOException {
URL url = new URL(imageUrl);
BufferedImage image = ImageIO.read(url);
return image;
}
这应该从给定的 URL 返回图像。
我用这两个随机选择的 URL 进行了测试:
- https://www.google.co.ma/images/srpr/logo4w.png
- http://www.earthtimes.org/newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg
第一个工作正常,但第二个给出 403 错误:
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.earthtimes.org/newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at java.net.URL.openStream(URL.java:1010)
at javax.imageio.ImageIO.read(ImageIO.java:1367)
错误的原因可能是什么?谢谢。