简体   繁体   English

无法从URL获取输入流! 爪哇

[英]Can't get input stream from URL! Java

I have the following simple program which reads an image from a url. 我有以下简单程序,可从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 我有这个问题是,这给了例外

Can't get input stream from URL! 无法从URL获取输入流!

with an message: 并显示一条消息:

Received fatal alert: internal_error 收到致命警报: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? 此错误仅适用于该URL中的图像,但是有人知道如何解决此问题吗?

You set the User-Agent on the connection but then use the imageUrl to load the image, so this has no effect. 您在连接上设置了User-Agent ,但随后使用imageUrl加载了图像,因此这无效。

Use 采用

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

instead (and close the inputstream once you are done). 相反(并在完成后关闭输入流)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM