File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Image extension and mimetype Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Image extension and mimetype" Watch "Image extension and mimetype" New topic

Image extension and mimetype

gani vojja

Joined: Oct 12, 2011
Posts: 1
Hey Guys,

Is there any way i could find out the image extension and mimtype from stream(or uploaded file)?

final InputStream is = dataHandler.getInputStream();
final BufferedImage bi =;

System.out.println("width: " + bi.getWidth());
System.out.println("height: " + bi.getHeight());
System.out.println("type" + bi.getType());

I can use the Magic.getMagicmatch to get the mime type?
//final String mimeType = Magic.getMagicMatch(BYTESTREAM, false).getMimeType();

but, is there any way to get the extension from uploaded file using java?

Rob Spoor

Joined: Oct 27, 2005
Posts: 19908

Use String's lastIndexOf method to find the last location of a dot, then return the substring from that location + 1. Be careful that lastIndexOf will return -1 when no dot is found, which also means there is no extension.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Image extension and mimetype