Can't get input stream from URL! Java

筅森魡賤 提交于 2019-12-13 04:26:54

问题


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

Can't get input stream from URL!

with an message:

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?


回答1:


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

Use

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

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



来源:https://stackoverflow.com/questions/35430198/cant-get-input-stream-from-url-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!