简体   繁体   中英

Is there a way to read the mime types file with Java Properties?

I'm trying to read the mime types file with Java Properties to get the appropriate mime type according to a files extension. However, the file is laid out such that I can only get the file extension for a mime type not the other way round.

For example:

text/html                   html htm

as opposed to the desired format:

html htm                    text/html

Therefore, if I read it with a Properties object like so:

mimeTypes.getProperty("text/html");

I would get html and htm but if I do it the other round:

mimeTypes.getProperty("html");

I would not get text/html which is what I want. So, is there anyway to get the key by providing the key's value?

Thanks for any help and sorry if question is out of order or anything.

Cheers

EDIT:

No, I will not be using this:

FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(filename);  

I want to do it the long way because I'm hardcore like that :)

you can use the entrySet() function to get the Mappings (key,value). Then iterate over that set. I don't think there is a direct method in the collections api to get a key from value.

It sounds like you want to read in the mimetypes file, parse it, and put it into a "reversed" HashMap of extension:type (rather than type:extensions). You could still read it in with Properties, just once you have it, you would then need to parse it into the reversed HashMap. I think this would make your code much cleaner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM