无法从URL获取输入流!爪哇 [英] Can't get input stream from URL! Java

查看:509
本文介绍了无法从URL获取输入流!爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的程序,可从url读取图像.

I have the following simple program which reads an image from a url.

public class TestWebScrapper {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        URL imageUrl;
        try {
            imageUrl = new URL("https://assets.boxdice.com.au/bj_corporate/listings/1751/4618116b.jpg");
            HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
                    connection.setRequestProperty(
                            "User-Agent",
                            "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0");
            BufferedImage propertImage = ImageIO.read(imageUrl);
        } catch (Exception ex) {
            Logger.getLogger(TestWebScrapper.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

我遇到的问题是,这给了例外

The issue that I have this is that this gives an exception

无法从URL获取输入流!

Can't get input stream from URL!

并显示一条消息:

收到的致命警报:internal_error

Received fatal alert: internal_error

我尝试添加用户代理,但问题仍然存在.该链接在浏览器上可以正常工作. 此错误仅适用于该网址中的图片,但是有人知道如何解决此问题吗?

I tried adding the user-agent, but the issue persists. The link works fine on a browser. This error comes only for images from this url, but does anyone know how to work around this?

推荐答案

您在连接上设置了User-Agent,但随后使用imageUrl加载了图像,因此无效.

You set the User-Agent on the connection but then use the imageUrl to load the image, so this has no effect.

使用

BufferedImage propertImage = ImageIO.read(connection.getInputStream());

代替(并在完成后关闭输入流).

instead (and close the inputstream once you are done).

这篇关于无法从URL获取输入流!爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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