I'm looking for some code to get the dimensions of an image on a website in Java without having to download it first. The idea is that if the image is too big, I wouldn't download the file and would return an error message, so users don't input URLs that lead to files which are too big. It would take the height and the width of the image.
I know this code, from another user on StackOverflow, does this when you have a file on the system:
private Dimension getImageDim(final String path) {
Dimension result = null;
String suffix = this.getFileSuffix(path);
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try {
ImageInputStream stream = new FileImageInputStream(new File(path));
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
result = new Dimension(width, height);
} catch (IOException e) {
log(e.getMessage());
} finally {
reader.dispose();
}
} else {
log("No reader found for given format: " + suffix));
}
return result; }
However, I have no idea how to translate that into something that can be used on a URL object.
Edit: I'm interested in the pixel width and the pixel height.