2

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?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
gdfbarbosa
  • 825
  • 3
  • 10
  • 21

2 Answers2

4

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));
nandu
  • 779
  • 4
  • 18
0

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());
    }
MikkoP
  • 4,864
  • 16
  • 58
  • 106