read java image from url and save to byte array

Howto create an image object from url (file, http, etc.) and the basics to get it saved to stream – here to byte array

    package dummy;

    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;

    public class ImageReader {

        public void main(String[] args) throws Exception {

            URL url = new URL("");

            // read image direct from url
            BufferedImage image =;

            // write image to outputstream
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", baos);

            // get bytes
            byte[] imageBytes = baos.toByteArray();