import java.awt.image.BufferedImage; import java.net.URL; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; public class Main { public static void main(String[] argv) throws Exception { ImageInputStream imageStream = ImageIO.createImageInputStream(new URL("").openStream()); Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream); ImageReader reader = null; if (!readers.hasNext()) { imageStream.close(); return; } else { reader = readers.next(); } String formatName = reader.getFormatName(); if (!formatName.equalsIgnoreCase("jpeg") && !formatName.equalsIgnoreCase("png") && !formatName.equalsIgnoreCase("gif")) { imageStream.close(); return; } reader.setInput(imageStream, true, true); BufferedImage theImage = reader.read(0); reader.dispose(); imageStream.close(); } }