Example usage for javax.activation MimetypesFileTypeMap getContentType

List of usage examples for javax.activation MimetypesFileTypeMap getContentType

Introduction

In this page you can find the example usage for javax.activation MimetypesFileTypeMap getContentType.

Prototype

public synchronized String getContentType(String filename) 

Source Link

Document

Return the MIME type based on the specified file name.

Usage

From source file:com.playonlinux.core.utils.FileAnalyser.java

public static String getMimetype(File inputFile) throws PlayOnLinuxException {
    try {//from  w w w .  java2  s.co  m
        return getMatch(inputFile).getMimeType();
    } catch (MagicMatchNotFoundException e) {
        LOGGER.debug(e);
        final MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        return mimeTypesMap.getContentType(inputFile);
    }
}

From source file:org.silverpeas.mobile.server.helpers.DataURLHelper.java

/**
 * Return avatar dataurl//from  w w w .  ja  v  a  2  s. c o  m
 * @param photoFileName
 * @return
 */
public static String convertAvatarToUrlData(String photoFileName, String size) {

    int i = photoFileName.lastIndexOf("/");
    if (i == -1)
        i = photoFileName.lastIndexOf("\\");
    if (i != -1) {
        photoFileName = photoFileName.substring(i + 1);
    }

    String data = "";
    try {
        File originalImage = new File(
                FileRepositoryManager.getAvatarPath() + File.separatorChar + photoFileName);
        if (!originalImage.exists()) {
            return "";
        }

        String askedPath = originalImage.getParent() + File.separator + size + File.separator + photoFileName;
        SilverpeasFile image = SilverpeasFileProvider.getFile(askedPath);

        byte[] binaryData = IOUtils.toByteArray(image.inputStream());
        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        data = "data:" + mimeTypesMap.getContentType(originalImage) + ";base64,"
                + new String(Base64.encodeBase64(binaryData));
    } catch (Exception e) {
        SilverLogger.getLogger(SpMobileLogModule.getName())
                .error("PublicationContentServlet.convertSpImageUrlToDataUrl", "root.EX_NO_MESSAGE", e);
    }
    return data;
}

From source file:nz.net.paulo.RequestHandler.java

protected static String getMimeType(final File file) {
    String result = "";
    if (file.exists()) {
        if (is(file, "png")) {
            result = "image/png";
        } else if (is(file, "jpg")) {
            result = "image/jpg";
        } else if (is(file, "jpeg")) {
            result = "image/jpeg";
        } else if (is(file, "gif")) {
            result = "image/gif";
        } else {/*from  w w  w  . j  ava2 s . c o m*/
            // if we used ConfigurableMimeFileTypeMap from Spring we wouldn't have to do all the prior cases
            javax.activation.MimetypesFileTypeMap mtMap = new javax.activation.MimetypesFileTypeMap();
            result = mtMap.getContentType(file);
        }
    }
    return result;
}

From source file:org.apache.tajo.storage.http.ExampleHttpServerHandler.java

/**
 * Sets the content type header for the HTTP Response
 * @param response HTTP response//from  ww w. j av a 2 s .co m
 * @param file file to extract content type
 */
private static void setContentTypeHeader(HttpResponse response, File file) {
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    response.headers().set(CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
}

From source file:ro.ieugen.fileserver.http.HttpStaticFileServerHandler.java

/**
 * Sets the content type header for the HTTP Response
 *
 * @param response HTTP response//from   w  w  w. jav  a  2s. com
 * @param file     file to extract content type
 */
private static void setContentTypeHeader(HttpResponse response, File file) {
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    response.setHeader(CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
}

From source file:com.qmetry.qaf.automation.util.FileUtil.java

public static String getContentType(File f) {
    MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String ct = fileNameMap.getContentTypeFor(f.getName());
    return StringUtil.isBlank(ct) ? fileTypeMap.getContentType(f) : ct;
}

From source file:com.nextcont.ecm.fileengine.http.nettyServer.HttpUploadServerHandler.java

private static void setContentTypeHeader(HttpResponse response, FileRecord file) {
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    response.headers().set(CONTENT_TYPE,
            mimeTypesMap.getContentType(file.getFullName()) + "; charset=" + file.getHttpEncoding());
}

From source file:org.mycore.common.xml.MCRXMLFunctions.java

/**
 * Determines the mime type for the file given by its name.
 *
 * @param f//from   w ww  .j ava  2 s  .  c  om
 *            the name of the file
 * @return the mime type of the given file
 */
public static String getMimeType(String f) {
    if (f == null) {
        return "application/octet-stream";
    }
    MimetypesFileTypeMap mTypes = new MimetypesFileTypeMap();
    return mTypes.getContentType(f.toLowerCase(Locale.ROOT));
}

From source file:org.phoenicis.tools.files.FileAnalyser.java

public String getMimetype(File inputFile) {
    try {/*from w  w  w  . j  a  v a  2  s  .c om*/
        final MagicMatch match = getMatch(inputFile);
        final String mimeType = match.getMimeType();
        if ("???".equals(mimeType)) {
            return guessMimeTypeFromDescription(match);
        }

        return mimeType;
    } catch (MagicMatchNotFoundException e) {
        LOGGER.debug("Failed to get Mime Type", e);
        final MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        return mimeTypesMap.getContentType(inputFile);
    }
}

From source file:org.sakaiproject.nakamura.docproxy.disk.DiskDocumentResult.java

/**
 * {@inheritDoc}//from   w  w  w.ja v a 2  s . c om
 * 
 * @see org.sakaiproject.kernel.api.docproxy.ExternalDocumentResultMetadata#getContentType()
 */
public String getContentType() {
    MimetypesFileTypeMap map = new MimetypesFileTypeMap();
    return map.getContentType(file);
}