Search code examples
javaurlbufferedimagejavax.imageiodiscord-jda

Can't get input stream from URL


I'm trying to get a BufferedImage from an URL, but it throws me an IIOException. I already checked the URL and It works fine.

Exception:

javax.imageio.IIOException: Can't get input stream from URL!
javax.imageio.ImageIO.read(ImageIO.java:1395)
de.visionvenue.statify.objects.Statistics.<init>(Statistics.java:60)
de.visionvenue.statify.commands.SetupCommand.onCommand(SetupCommand.java:67)
de.visionvenue.statify.manager.CommandManager.lambda$0(CommandManager.java:26)
java.lang.Thread.run(Thread.java:748)

Here is the code that where I try to get the BufferedImage

URL img = new URL(settings.getGuild().getIconUrl());
            
BufferedImage image = ImageIO.read(img);

The URL is

https://cdn.discordapp.com/icons/712313516542918717/8b1e61da607cdd67546ffba653a12604.png

Solution

  • Given the few code fragments, it is hard to see the cause as it works for other users as seen in the comments.


    Original answer:

    You want to use the URLConnection class instead.

    You can then call .connect() and .getInputStream().


    If you need more sophisticated logic and better APIs, I would recommend to use either Apache HTTPClient (for any HTTP calls) or the JAX-RS client which is best suited for REST-based connections, but can also be used for any other url connection. The advantage of the latter is that multiple implementations exist (resteasy, jersey, CXF, just to name a few).