Java Utililty Methods Mime Type Get

List of utility methods to do Mime Type Get

Description

The list of methods to do Mime Type Get are organized into topic(s).

Method

StringextractMimeType(byte[] imageData)
extract Mime Type
try (ByteArrayInputStream input = new ByteArrayInputStream(imageData)) {
    String contentType = URLConnection.guessContentTypeFromStream(input);
    return contentType != null ? contentType : "image/xyz";
StringgetExactMimeType(String fileName)
This gets the MIME type of a file, based on its name.
if (fileName == null)
    return null;
String mimeType = URLConnection.getFileNameMap().getContentTypeFor(fileName);
if (mimeType == null) {
    try {
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        if ("js".equals(suffix))
            mimeType = "text/javascript";
...
StringgetMimeType(File file)
Try to detect MimeType by backwards compatible methods
String guess = null;
if (file != null && file.exists() && file.isFile()) {
    InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(file));
        guess = URLConnection.guessContentTypeFromStream(is);
    } catch (IOException e) {
        e.printStackTrace();
...
StringgetMimeType(String file)
get Mime Type
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String type = fileNameMap.getContentTypeFor(file);
return type;
StringgetMimeType(String fileName)
Attempts to determine the MIME type of the specified file.
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(fileName);
if (mimeType != null)
    return mimeType;
return "application/octet-stream";
StringgetMIMEType(String fileName)
Returns the MIME type of the given file name.
FileNameMap fileNameMap = URLConnection.getFileNameMap();
return fileNameMap.getContentTypeFor(fileName);
StringgetMimeType(String fileName)
Get the mime type for the specified filename
if (fileName.contains(".")) {
    String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
    if (mimeTypes.containsKey(extension)) {
        return mimeTypes.get(extension);
String defaultMime = fileNameMap.getContentTypeFor(fileName);
if (defaultMime != null) {
...
StringgetMimeType(String fileName)
This gets the MIME type of a file, based on its name.
if (fileName == null)
    return null;
String mimeType = getExactMimeType(fileName);
if (mimeType == null)
    mimeType = "untyped/binary";
return mimeType;
StringgetMimeTypeByFileName(String fileName)
get Mime Type By File Name
if (fileName == null)
    return DEFAULT_MIME_TYPE;
String en = URLConnection.getFileNameMap().getContentTypeFor(fileName);
return en != null ? en : DEFAULT_MIME_TYPE;
StringgetMimeTypeForFile(File memberFile)
get Mime Type For File
URL fileUrl;
String mimeType = "";
try {
    fileUrl = memberFile.toURI().toURL();
    URLConnection c = fileUrl.openConnection();
    mimeType = c.getContentType();
} catch (MalformedURLException e) {
    e.printStackTrace();
...