Find the size of a jpg image in java

Hi,
I have an s3 url which is of the image in jpg format. I have the following code :
        BufferedImage image = ImageIO.read(new URL(url));
I need to calculate the sizeof this image.
How do i do that.
I tried the following :
 public static Integer getSize(BufferedImage bufferedImage) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
    byteArrayOutputStream.close();
    return byteArrayOutputStream.size();
  }

Open in new window

It gave 18183 bytes for an image of 14Kb which is wrong.

Mainly i need to detect if the size of the image is more than 1 MB.

Please suggest any ways to do it in java or any alternatives to achieve this ?

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
There won't necessarily be identity between the original file size and the size reported by size(). If you need that, you should use the original datum or accept the possible inaccuracy

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
what do you mean by original datum ?
CEHJCommented:
The original file size of the image (if you have the original file)
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Rohit BajajAuthor Commented:
I tried this :
    URL url = new URL(urlofImage);
URLConnection conn = url.openConnection();
    System.out.println(conn.getContentLength());

This seems to be working exactly...
Any views on this approach ??
CEHJCommented:
That is essentially the same as i suggested
CEHJCommented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Images and Photos

From novice to tech pro — start learning today.