ImageIO.read()返回403错误 [英] ImageIO.read() returns 403 error

查看:394
本文介绍了ImageIO.read()返回403错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

第一个工作正常,但第二个产生403错误:

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.

推荐答案

ImageIO.read(URL)方法打开一个几乎所有默认设置的URL连接,包括 User-Agent 属性(将设置为您运行的JVM版本)。显然,您列出的网站需要更标准的UA。使用直接telnet连接进行测试:

The ImageIO.read(URL) method opens a URL connection with pretty much all default settings, including the User-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:

请求发送 ImageIO.read(url)


GET /newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg HTTP / 1.1

User-Agent: Java / 1.7.0_17

主持人:www.earthtimes.org

接受:text / html,image / gif,image / jpeg,*; q = .2, / ; q = .2

连接:keep-alive

GET /newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg HTTP/1.1
User-Agent: Java/1.7.0_17
Host: www.earthtimes.org
Accept: text/html, image/gif, image/jpeg, *; q=.2, /; q=.2
Connection: keep-alive

响应代码为404(对我来说至少),返回默认 text / html 页面。

请求已发送通过'标准'浏览器:

Request sent by 'standard' browser:


GET /newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg HTTP / 1.1

用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_7_5)AppleWebKit / 537.31(KHTML,与Gecko一样)Chrome / 26.0.1410.65 Safari / 537.31

主机:www .earthtimes.org

接受:text / html,image / gif,image / jpeg,*; q = .2, / ; q = .2

连接:keep-alive

GET /newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg HTTP/1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31
Host: www.earthtimes.org
Accept: text/html, image/gif, image/jpeg, *; q=.2, /; q=.2
Connection: keep-alive

响应代码为200,使用图像数据。

以下简单修复程序可以延长代码,但通过设置更标准的UA来解决问题:

The following simple fix lengthens your code, but gets around the problem, by setting a more 'standard' UA:

final String urlStr = "http://www.earthtimes.org/newsimage/osteoderms-storing-minerals-helped-huge-dinosaurs-survive_3011.jpg";
final URL url = new URL(urlStr);
final HttpURLConnection connection = (HttpURLConnection) url
        .openConnection();
connection.setRequestProperty(
    "User-Agent",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
final BufferedImage image = ImageIO.read(connection.getInputStream());

这篇关于ImageIO.read()返回403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆