Get the extension from a MimeType

后端 未结 5 424
不思量自难忘°
不思量自难忘° 2020-12-16 23:27

I want to get the extension from a MimeType.

For example:

video/mp4                       ---->  mp4
application/x         


        
5条回答
  •  时光说笑
    2020-12-17 00:03

    Using the suggestion of "Mindaugas Bernatavičius", I created a class to resolve my problem.


    import java.util.HashMap;
    
    public class MimeTypesAndExtensions {
    
        static HashMap MimesExtensions_HashMap = new HashMap();
    
        public MimeTypesAndExtensions() {
            super();
        }
    
        static {
            createHashMap();
        }
    
        /*****************************************************************************
         Extracted from : https://www.freeformatter.com/mime-types-list.html
    
         - The "x-" prefix of a MIME subtype simply means that it's non-standard,
           i.e. not registered with the "Internet Assigned Numbers Authority" (IANA).
         - The "vnd" prefix means that the MIME value is vendor specific.
        *****************************************************************************/
        private static void createHashMap() {
    
            // This list is not complete... 
            // I got it from https://www.freeformatter.com/mime-types-list.html
            MimesExtensions_HashMap.put("application/vnd.hzn-3d-crossword", "x3d");
            MimesExtensions_HashMap.put("application/vnd.lotus-1-2-3", "123");
            MimesExtensions_HashMap.put("text/vnd.in3d.3dml", "3dml");
            MimesExtensions_HashMap.put("video/3gpp2", "3g2");
            .....       
            MimesExtensions_HashMap.put("application/yin+xml", "yin");
            MimesExtensions_HashMap.put("application/vnd.zzazz.deck+xml", "zaz");
            MimesExtensions_HashMap.put("application/zip", "zip");
            MimesExtensions_HashMap.put("application/vnd.zul", "zir");
            MimesExtensions_HashMap.put("application/vnd.handheld-entertainment+xml", "zmm");
            MimesExtensions_HashMap.put("application/andrew-inset", "N/A");
        }
    
        public static String getExtensionFromMimeType(String mimeType) {
    
            String extension = "Extension Not Found";
            for(String key: MimesExtensions_HashMap.keySet()){
                if(key.toLowerCase().equals(mimeType.toLowerCase())) {
                    extension = MimesExtensions_HashMap.get(key);
                    return extension;
                }
            }
    
            return extension;
        }
    
        public static String getMimeTypeFromExtension(String extension) {
    
            StringBuilder mimeTypes = new StringBuilder();
            for(String key: MimesExtensions_HashMap.keySet()){
                if(MimesExtensions_HashMap.get(key.toLowerCase()).equals(extension.toLowerCase())) {
                    mimeTypes.append(key.concat("\n"));
                }
            }
    
            if(mimeTypes.length() == 0)
                return "MimeType Not Found";
    
            return mimeTypes.toString();
        }
    
        public static String getMimeTypesAndExtensions(){
    
            StringBuilder mimeTypesAndExtensions = new StringBuilder();
            for(String key: MimesExtensions_HashMap.keySet()){
                mimeTypesAndExtensions.append(MimesExtensions_HashMap.get(key).concat("   ----->   ").concat(key).concat("\n"));
            }
    
            return mimeTypesAndExtensions.toString();
        }
    }
    

提交回复
热议问题