编程语言
首页 > 编程语言> > java-ImageIO.read()返回403错误

java-ImageIO.read()返回403错误

作者:互联网

我有以下代码:

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)

错误的原因可能是什么?谢谢.

解决方法:

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

ImageIO.read(url)发送的请求:

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页面.

“标准”浏览器发送的请求:

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来延长代码长度,但可以解决问题:

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());

标签:javax-imageio,java,url,http,http-status-code-403
来源: https://codeday.me/bug/20191010/1888663.html