Skip to content

Instantly share code, notes, and snippets.

@ezekielbaniaga
Created July 28, 2017 09:10
Show Gist options
  • Save ezekielbaniaga/8c1b197cae73c8f86f7e0cb1a7c6c263 to your computer and use it in GitHub Desktop.
Save ezekielbaniaga/8c1b197cae73c8f86f7e0cb1a7c6c263 to your computer and use it in GitHub Desktop.
Get MIME Type in Java
package blog.boydeploy.mimetype;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Objects;
public class MimeUtility {
public static String getMimeType(File file) throws FileNotFoundException {
Objects.requireNonNull(file);
if (!file.exists()) throw new FileNotFoundException("File is missing: " + file.getAbsolutePath());
FileNameMap fmap = URLConnection.getFileNameMap();
return fmap.getContentTypeFor(file.toURI().getPath());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment