Можно так:
sun.net.www.MimeTable t = sun.net.www.MimeTable.getDefaultTable();
MimeEntry e = t.find("text/plain");
System.out.println(e.getExtensionsAsList());
Результат:
.text,.c,.cc,.c++,.h,.pl,.txt,.java,.el
sun.net.
www.MimeTable по умолчанию использует данные из файла .../jre/lib/content-types.properties. Но, во-первых, этот файл не обновлялся, кажется, с версии java 1.2, и там не все MIME-типы определены (хотя можно вручную все добавить). А во-вторых, такой метод дает несколько расширений, а мне надо было одно. Поэтому делала вручную:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
public class ExtensionDefiner {
static private Map properties = null;
public static void init() {
FileInputStream in = null;
try {
URL url = ClassLoader.getSystemClassLoader().getResource(
"mimetypes.properties");
if (url != null)
in = new FileInputStream(url.getFile());
} catch (IOException e) {
e.printStackTrace();
}
if (in == null)
try {
in = new FileInputStream("mimetypes.properties");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (in != null) {
try {
Properties props = new Properties();
props.load(in);
in.close();
properties = props;
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String getExtensionFromMimeType(String mimeType) {
if (properties == null)
return null;
return (String) properties.get(mimeType);
}
// public static void main(String[] args) throws Exception {
// ExtensionDefiner.init();
// System.out.println(ExtensionDefiner.getExtensionFromMimeType("audio/x-wav"));
// }
}
Фрагмент из файла "mimetypes.properties":
...
audio/x-pn-realaudio=.ra
image/bmp=.bmp
image/cis-cod=.cod
...
Ну все. Ничего особенного :)