Reputation: 845
I need to know a file extension without having the file extension? Is this possible with the java IO API?
I'm getting the following exception while reading a JPG file:
javax.imageio.IIOException: Unsupported Image Type
Does anyone have any ideas how to solve this?
When i try to open the image in a normal image viewer in Mac OS X or Windows, it works just fine.
Is there any chances the file was renamed to a different type and it's original type is different from the extension?
Upvotes: 2
Views: 3149
Reputation: 111
You can use mime-util library. My sample code will get the mime type of the file.
String imagePath = "filepath";
File file = new File(imagePath);
if (MimeUtil.getMimeDetector(MagicMimeMimeDetector.class.getName()) == null) {
MimeUtil.registerMimeDetector(MagicMimeMimeDetector.class.getName());
}
Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(file);
for (MimeType mimeType : mimeTypes) {
System.out.println(mimeType.getMediaType());
}
Upvotes: 0
Reputation: 779
You can get the mime type with this code.
String imageName = "C:\\Documents and Settings\\home\\Desktop\\P092870.jpg";
File input = new File(imageName);
System.out.println("System Type description of " + input.getName() + " is " +
FileSystemView.getFileSystemView().getSystemTypeDescription(input));
System.out.println("Mime Type of " + input.getName() + " is " +
new MimetypesFileTypeMap().getContentType(input));
Upvotes: 4