Java Utililty Methods Content Type Get

List of utility methods to do Content Type Get

Description

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

Method

StringgetContentType(File file)
get Content Type
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null || !contentType.startsWith("image/")) {
    throw new IllegalArgumentException(file.getCanonicalPath() + " is not a supported image file.");
return contentType;
StringgetContentType(File file)
get Content Type
String contentType = (URLConnection.guessContentTypeFromName(file.getName()));
if ("".equals(contentType) || contentType == null) {
    if (file.getName().contains(".jhtml")) {
        contentType = "text/jhtml";
    if (file.getName().contains(".jap")) {
        contentType = "application/jap";
    if (file.getName().contains(".css")) {
        contentType = "text/css";
    if (file.getName().contains(".js")) {
        contentType = "text/javascript";
return contentType;
StringgetContentType(final File file)
Get a file mime type based on its filename.
return getContentType(file.getName());
StringgetContentType(final String filename)
Return the contentType for a given filename if it is defined inside the JVM
FileNameMap mapNew = URLConnection.getFileNameMap();
String temp = mapNew.getContentTypeFor("." + getExtension(filename));
return (temp == null) ? "application/octet-stream" : temp;
StringgetContentType(String boundary)
Gets the content type string suitable for the java.net.URLConnection which includes the multipart boundary string.
return "multipart/form-data; boundary=" + boundary;
StringgetContentType(String file)
Gets the content type for the specified file.
String foundContentType = null;
file = file.trim();
if (fileNameMap != null)
    foundContentType = fileNameMap.getContentTypeFor(file);
if (foundContentType != null)
    return foundContentType;
else
    return "application/octet-stream";
...
StringgetContentType(String fileName)
get Content Type
return URLConnection.guessContentTypeFromName(fileName);
StringgetContentType(String filename)
get Content Type
return filename.endsWith(".html") ? "text/html"
        : filename.endsWith(".css") ? "text/css"
                : filename.endsWith(".js") ? "text/javascript"
                        : URLConnection.guessContentTypeFromName(filename);
StringgetContentType(String name)
Gets the content type for the specified filename.
String contentType;
FileNameMap fileNameMap = URLConnection.getFileNameMap();
contentType = fileNameMap.getContentTypeFor(name);
if (contentType == null) {
    int index = name.lastIndexOf(".");
    if (index > -1) {
        contentType = localFileNameMap.getProperty(name.substring(index));
return contentType;