I have the following code:
public BufferedImage urlToImage(String imageUrl) throws MalformedURLException, IOException {
URL url = new URL(imageUrl);
BufferedImage image = ImageIO.read(url);
return image;
}
That is supposed to return an image from a given URL.
I tested with these two randomly chosen URLs:
- https://www.google.co.ma/images/srpr/logo4w.png
- http://www.earthtimes.org/newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg
The first one works fine, but the second gives a 403 error:
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)
What could be the cause of the error ? Thanks.
The
ImageIO.read(URL)
method opens a URL connection with pretty much all default settings, including theUser-Agent
property (which will be set to the JVM version you are running on). Apparently, the site you listed expects a more 'standard' UA. Testing with a straight telnet connection:Request sent by
ImageIO.read(url)
:Response code is 404 (for me at least), with a default
text/html
page being returned.Request sent by 'standard' browser:
Response code is 200, with the image data.
The following simple fix lengthens your code, but gets around the problem, by setting a more 'standard' UA: