Example usage for java.net FileNameMap getContentTypeFor

List of usage examples for java.net FileNameMap getContentTypeFor

Introduction

In this page you can find the example usage for java.net FileNameMap getContentTypeFor.

Prototype

public String getContentTypeFor(String fileName);

Source Link

Document

Gets the MIME type for the specified file name.

Usage

From source file:Main.java

public static String getMimeTypeForFile(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    return fileNameMap.getContentTypeFor(path);
}

From source file:Main.java

public static String getFileType(String fn, String defaultType) {
    FileNameMap fNameMap = URLConnection.getFileNameMap();
    String type = fNameMap.getContentTypeFor(fn);
    return type == null ? defaultType : type;
}

From source file:Main.java

public static String guessMimeType(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = fileNameMap.getContentTypeFor(path);
    if (contentTypeFor == null) {
        contentTypeFor = "application/octet-stream";
    }/*from  w  ww .  java 2s .  com*/
    return contentTypeFor;
}

From source file:Main.java

private static String guessMimeType(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = fileNameMap.getContentTypeFor(path);
    if (contentTypeFor == null) {
        contentTypeFor = "application/octet-stream";
    }//from   w w  w.j a v  a  2  s. c o m
    return contentTypeFor;
}

From source file:Main.java

public static String getMimeType(String filename) {
    FileNameMap filenameMap = URLConnection.getFileNameMap();
    String contentTypeFor = filenameMap.getContentTypeFor(filename);
    if (contentTypeFor == null) {
        contentTypeFor = "application/octet-stream";
    }//from  ww  w . j a  v  a 2 s  . c  o  m
    return contentTypeFor;
}

From source file:Main.java

private static String getMimeType(String filename) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = fileNameMap.getContentTypeFor(filename);
    if (contentTypeFor == null) {
        contentTypeFor = "application/octet-stream";
    }//from w  w w  .ja  v  a  2  s  . co m
    return contentTypeFor;
}

From source file:Main.java

public static String getMimeType(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = fileNameMap.getContentTypeFor(path);
    if (contentTypeFor == null) {
        contentTypeFor = "application/octet-stream";
    }/*from w  w  w  .  j  a  va 2  s  .c o m*/
    return contentTypeFor;
}

From source file:nl.clockwork.common.util.Utils.java

public static String getMimeType(String fileName) throws java.io.IOException {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    return fileNameMap.getContentTypeFor(fileName);
}

From source file:com.discovery.darchrow.io.MimeTypeUtil.java

/**
 *  content type by file name.<br>
 * //from ww  w . j av  a 2s .c o m
 * //TODO
 * <b>
 * Very incomplete function. As of Java 7, html, pdf and jpeg extensions return the correct mime-type but js and css return null! </b> <br>
 * 
 * <p>
 * I tried Apache Tika but it is huge with tons of dependencies, <br>
 * URLConnection doesn't use the bytes of the files, <br>
 * MimetypesFileTypeMap also just looks at files names,<br>
 * and I couldn't move to Java 7.
 * </p>
 * 
 * @param fileName
 *            the file name
 * @return the content type by file name
 * @see java.net.URLConnection#getFileNameMap()
 * @see javax.activation.MimetypesFileTypeMap#getContentType(java.io.File)
 * @see java.net.FileNameMap#getContentTypeFor(String)
 * @see org.apache.commons.io.FilenameUtils#getExtension(String)
 * @see java.net.URLConnection#guessContentTypeFromName(String)
 * @see java.net.URLConnection#guessContentTypeFromStream(java.io.InputStream)
 * @see "java.nio.file.Files#probeContentType(java.nio.file.Path)"
 */
public static String getContentTypeByFileName(String fileName) {

    String extension = FilenameUtils.getExtension(fileName);
    if (Validator.isNullOrEmpty(extension)) {
        return null;
    }

    // 1. first use java's build-in utils
    //??? mimetable? "content.types.user.table" ?? java  lib/content-types.properties 
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentType = fileNameMap.getContentTypeFor(fileName);

    // 2. nothing found -> lookup our in extension map to find types like ".doc" or ".docx"
    if (Validator.isNullOrEmpty(contentType)) {
        contentType = fileExtensionMap.get(extension.toLowerCase());
    }

    return contentType;
}

From source file:com.sunchenbin.store.feilong.core.io.MimeTypeUtil.java

/**
 *  content type by file name./*from   w  w  w.j  a v a 2s  .c o m*/
 * 
 * <p>
 * <b>Very incomplete function. As of Java 7, html, pdf and jpeg extensions return the correct mime-type but js and css return null!
 * </b>
 * </p>
 * 
 * <p>
 * I tried Apache Tika but it is huge with tons of dependencies, <br>
 * URLConnection doesn't use the bytes of the files, <br>
 * {@link MimetypesFileTypeMap} also just looks at files names,and I couldn't move to Java 7.
 * </p>
 * 
 * @param fileName
 *            the file name
 * @return the content type by file name
 * @see java.net.URLConnection#getFileNameMap()
 * @see javax.activation.MimetypesFileTypeMap#getContentType(java.io.File)
 * @see java.net.FileNameMap#getContentTypeFor(String)
 * @see org.apache.commons.io.FilenameUtils#getExtension(String)
 * @see java.net.URLConnection#guessContentTypeFromName(String)
 * @see java.net.URLConnection#guessContentTypeFromStream(java.io.InputStream)
 * @see "java.nio.file.Files#probeContentType(java.nio.file.Path)"
 */
public static String getContentTypeByFileName(String fileName) {

    String extension = FilenameUtils.getExtension(fileName);
    if (Validator.isNullOrEmpty(extension)) {
        return StringUtils.EMPTY;
    }

    // 1. first use java's build-in utils
    //??? mimetable? "content.types.user.table" ,?? java  lib/content-types.properties 
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentType = fileNameMap.getContentTypeFor(fileName);

    // 2. nothing found -> lookup our in extension map to find types like ".doc" or ".docx"
    if (Validator.isNullOrEmpty(contentType)) {
        contentType = fileExtensionMap.get(extension.toLowerCase());
    }

    return contentType;
}