Created
July 28, 2017 09:10
-
-
Save ezekielbaniaga/8c1b197cae73c8f86f7e0cb1a7c6c263 to your computer and use it in GitHub Desktop.
Get MIME Type in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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