If you are stuck with java 5-6 then this utility class from servoy open source product.
You only need this function
public static String getContentType(byte[] data, String name)
It probes the first bytes of the content and returns the content types based on that content and not by file extension.