简体   繁体   中英

Can't get input stream from URL! Java

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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