Extract file extension from mime type

I have been using:

URLConnection uc = url.openConnection();
String origMimeType = uc.getContentType();

to get the mimetype of a file from url. It will extract the mime type of the file based on JAVA_HOME/lib/content-types.properties file.

But now, I need an opposite method to get the file extension back from the file mime type.

Is there a method available to do this?

I saw this post:

But it's suggesting loading another "mimetype to file extension conversion" reference list. I would like to use the one I've used to be consistent which is the content-type.properties file.

If possible, I would avoid manually parse the file.

Here is part of the content-types.properties file:

application/octet-stream: \
      description=Generic Binary Stream;\

application/oda: \
      description=ODA Document;\

application/pdf: \
      description=Adobe PDF Format;\

Thanks a lot!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

There are only 60 in my version - is that really enough?
Well - what needs to be returned if more that one extenstion is specified for the type? (just trying to get the technical part cleared - I do not think that there is a ready to use mething)

If the file looks like above, just parse it... should not be that hard I think.
mybluegrassAuthor Commented:
I ended up using MimeTable to do this. Here is what I've done. If there are multiple extension returned, I just used the last one for now.

  public static String getFileExt(String mimeType) {
    String extension = "";

    MimeTable testTable = MimeTable.getDefaultTable();
    Enumeration e = testTable.elements();
    while (e.hasMoreElements()) {
      MimeEntry entry = (MimeEntry) e.nextElement();
      String contentType = entry.getType();
      String extensionString = entry.getExtensionsAsList();
      String[] extensionArray = extensionString.split(",");
      extensionString = extensionArray[extensionArray.length - 1];
      mimeType = mimeType.replaceAll("/", ".*");
      if (contentType.matches(mimeType)) {
        extension = extensionString;
    return extension;

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
Not such a good idea to use unsupported classes -you will get warnings and what's more the default doesn't contain many extensions anyway
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
Java EE

From novice to tech pro — start learning today.