How to determine the mime type of a file from its extension?

  • 3
    In general, no way. Expansion - a thing in general otfarnarnaya. - Akina
  • @Akina And then in general, how can you recognize him? - user235658

2 answers 2

We take the list of officially registered in IANA.ORG Media Types . Hardcode We are looking for the extension of a specific file in the table, we get the MIME type.

    In general, no way. Expansion - a thing in general otfarnarnaya

    Are you sure @Akina?

    There is Apache Tika - there you can simply get the mime type from any extension. There are many examples on the net - Google to help

    If we are talking about Android, then it is even easier - there is a standard class MimeTypeMap :

    String mimeType=MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 
    • RTFM: String: A file extension without the leading '.' - Barmaley
    • First I thought all the rules, but then I found out that the files with the Russian name do not determine - user235658
    • one
      Yes I am sure. Because ANY file I have the right to give ANY extension. And chach me on what's inside the file. In other words, the expansion maximum says "there is a possibility that", but there are no guarantees. In general, an extension is not a separate entity, but simply a piece of a name after the last point, if there is one (and if not, then the extension is empty, well, or we can say that it is missing), and it is not much more reliable to focus on it than, say, say that if the file name starts with wp_, then this is just a file from WordPress. - Akina
    • @Akina never doubt your ability to swipe at anything and anyone :) The point was that any mime type always corresponds to a certain mime type (this is not about content, but about content declaration, which is mime type) - Barmaley
    • @Barmaley The stated AUTHOR question asks to determine the mime type of the file from its extension . Specific file. I argue that between “must be according to expansion” and “is in fact” - in reality there can be a huge distance. It may not be. If these two options are the same - I gave a link to the match. If not match - indicated in the comments. What are you trying to say - I can not understand. Do you agree with one of these options? Or are you trying to describe a third one that I have missed? "The MIME type corresponds to the content, not the extension" - at least this is not disputed? - Akina