Java Content Type Get getContentType(String file)

Here you can find the source of getContentType(String file)

Description

Gets the content type for the specified file.

License

Apache License

Parameter

Parameter Description
file the name of the file to get the contenttype for.

Return

the content type.

Declaration

public static String getContentType(String file) 

Method Source Code

//package com.java2s;
/*/*  w w w  .j av  a  2  s  .  co  m*/
 * Copyright 2007 the project originators.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.FileNameMap;

public class Main {
    /** 
     * A static reference to a file name map. When loaded, this class will atempt to use the 
     * sun.net.www.MimeTable to load a filename map. If that class isn't available a default filename map, 
     * consisting of the most common types, will be created.
     */
    protected static FileNameMap fileNameMap = null;

    /**
     * Gets the content type for the specified file. This method uses the static FileNameMap member (@see #fileNameMap) to 
     * get the conten type for a certain file. If no type could be found, "application/octet-stream" will be returned. <br>
     * <br>
     * Subclasses can override this method to provide a better implementation.
     * 
     * @param file the name of the file to get the contenttype for.
     * 
     * @return the content type.
     */
    public static String getContentType(String file) {
        String foundContentType = null;
        file = file.trim();

        if (fileNameMap != null)
            foundContentType = fileNameMap.getContentTypeFor(file);

        if (foundContentType != null)
            return foundContentType;
        else
            return "application/octet-stream";
    }
}

Related

  1. getContentType(File file)
  2. getContentType(File file)
  3. getContentType(final File file)
  4. getContentType(final String filename)
  5. getContentType(String boundary)
  6. getContentType(String fileName)
  7. getContentType(String filename)
  8. getContentType(String name)