Java File Mime Type getImageType(File file)

Here you can find the source of getImageType(File file)

Description

get Image Type

License

Apache License

Declaration

public static String getImageType(File file) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;
import java.nio.file.Files;

import java.util.HashMap;

public class Main {
    private static HashMap<String, String> custommimetypes = new HashMap<String, String>();

    public static String getImageType(File file) {
        return getFileType(file).split("/")[1];
    }/*from  ww  w .ja  va2 s. c o m*/

    public static String getFileType(File file) {
        String extension = getExtension(file.getName());
        if (custommimetypes.containsKey(extension))
            return custommimetypes.get(extension);
        try {
            String content = Files.probeContentType(file.toPath());
            if (content != null)
                return content;
            else
                return "application/octet-stream";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "text/plain";
    }

    public static String getExtension(String filename) {
        if (filename.contains(".")) {
            return filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
        } else {
            return "";
        }
    }
}

Related

  1. detectFileType(String location)
  2. getFileType(String fileName)
  3. getMime(File file)
  4. getMimeType(File file)
  5. getMimeType(String fileName)
  6. getType(File file)