How to determine MIME type of file in java?

UPDATED: 01 October 2013
MIME type, File Type

Hello folks! How are you doing? Hope you are working hard on your code. Here I came up with the new article on how to determine the file type. This code will change your perspective about checking file types. If you are working on web application then it might help you.

Why file type filter is required?
When you deal with the file upload you have to be very careful on what type of file(s), user(s) allowed to upload. Usually we restrict our user to upload .exe, .msi, .bat, etc... executable file(s). Now think at different point of view. What if user change file extension from .exe to .png? This may create chaos.

Usual filter installed in our system.
Usually when user upload file(s) to server we check file type using something like fileItemTemp.getContentType(). This will give you file type which is determined using file extension but in real case its .exe file. You won't be able to determine the exact file type.

What is Magic header/array?
Magic Header is like identity of any file. You can't change it in any way for selected file(s) like .exe, .msi, .png, etc... This is what help us to determine the exact file type. At the very beginning of file you'll find this Magic Header sometime called Magic Array. However I didn't tested it for all file type so in some case it may not work. Java 7 built with solution of this issue so use it only when you are working below Java 7.
Java 7: Files.probeContentType(path)

Let have a look at the code.
import java.io.File;
import java.io.FileInputStream;

public class MIMECheck {
    public final int[] exeMagicHeader = new int[]{ 0x4d, 0x5a, 0x90, 0x00, 0x3, 0x00, 0x00, 0x00 };
    public final int[] msiMagicHeader = new int[]{ 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 }; 

    public static void main(String[] args) {
        MIMECheck mC = new MIMECheck();
        String rootPath = "D:\\javaQuery\\";

        File OriginalTempFile = new File(rootPath + File.separatorChar + "mysql-installer-5.5.25.0.msi");
        System.out.println(mC.isExe(OriginalTempFile));
        System.out.println(mC.isMsi(OriginalTempFile));
    }

    /**
     * Return true or false based on your input file
     * @param file
     * @return 
     */
    public boolean isExe(File file) {
        try {
            FileInputStream ins = new FileInputStream(file);
            for (int i = 0; i < 8; i++) {
                if (ins.read() != exeMagicHeader[i]) {
                    return false;
                }
            }
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
    
    /**
     * Return true or false based on your input file
     * @param file
     * @return 
     */
    public boolean isMsi(File file) {
        try {
            FileInputStream ins = new FileInputStream(file);
            for (int i = 0; i < 8; i++) {
                if (ins.read() != msiMagicHeader[i]) {
                    return false;
                }
            }
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * Generate Magic Array String of your input file
     * @param file 
     */
    public void generateMagicArray(File file) {
        String createMagicArray = "public final int[] MagicHeader = new int[] { ";
        try {
            FileInputStream ins = new FileInputStream(file);
            for (int i = 0; i < 8; i++) {
                createMagicArray += "0x" + Integer.toHexString(ins.read()) + ",";
            }
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        createMagicArray = createMagicArray.substring(0, createMagicArray.length() - 1);
        createMagicArray += " };";
        System.out.println(createMagicArray);
    }
}

Above code it self says a lot however lets understand each function.

- generateMagicArray will help you to create Magic Array any input file.
Note : You'll get different Magic Array of .txt, .bat, .reg, etc... for different input file. Reason behind it is Encoding method used by user to save file. You'll get same Magic Header for all .txt file which is created using ANSI. Same way for UTF-8 and UNICODE. Encode format decide the Magic Header.

- isExe and isMsi is used to compare the first 8 character of file and based on that will return true and false.

Now hope this article will solve your question regarding....
1) How to get file type in java?
2) How to determine MIME type in java?
3) How to get real file type in java?
4) How to verify file type in java?

0 comments :