Java File Mime Type detectFileType(String location)

Here you can find the source of detectFileType(String location)

Description

This function detects the file type of a file at the given location.

License

Open Source License

Parameter

Parameter Description
location the location of the file to detect the file type for.

Exception

Parameter Description
IOException if an I/O error occurs.

Return

a string indicating the file type of the file.

Declaration

public static String detectFileType(String location) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /**/*from  w w  w .j ava2s  . c  o  m*/
     * This function detects the file type of a file at the given location.
     * 
     * @param location
     *            the location of the file to detect the file type for.
     * @return a string indicating the file type of the file.
     * @throws IOException
     *             if an I/O error occurs.
     */
    public static String detectFileType(String location) throws IOException {
        String sanitizedLocation = location.replaceFirst("^/(.:/)", "$1");
        Path filePath = Paths.get(sanitizedLocation);
        return Files.probeContentType(filePath);
    }
}

Related

  1. getFileType(String fileName)
  2. getImageType(File file)
  3. getMime(File file)
  4. getMimeType(File file)