wood burning stoves*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Image extension and mimetype" Watch "Image extension and mimetype" New topic
Author

Image extension and mimetype

gani vojja
Greenhorn

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 = ImageIO.read(is);

//Dimentions
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
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Image extension and mimetype
 
Similar Threads
How to get the path of an Uploaded file?
FileDownloadServlet
problem with if-condition
Image Upload - Jsp or servlet
Issue with Copy and Paste from external source to applet.