Java get file type from content using mimeutil is not working as expected
In my following code:
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
File f = new File ("c:\\temp\\mime\\java.exe");
Collection<?> mimeTypes = MimeUtil.getMimeTypes("c:\\temp\\mime\\java.exe");
MimeType m = mimeTypes.toArray(new MimeType[mimeTypes.size()])[0];
System.out.println(m);

The output is always application/octet-stream no matter what file type is chosen ie csv, xls, exe, etc.
But according to the following site:
http://www.rgagnon.com/javadetails/java-0487.html
it should show as like ms-word or ms-excel etc.
How to make this work? All I want is just get the file type from file content(not by using file extension which is not very reliable). I was reading about other options like tika which requires too many files (like 20) which is too much for this single purpose and JMimeMagic which requires apache-oro which is a dead project hence I don't like the idea either. All other solutions seem to rely on file extension which seem not to be reliable as mentioned above.
Thanks

Did you see that http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java ?

Yes of course. The post refers exactly to the link I mentioned above and I just copy-pasted the code fragment from that site only.

Possibly related - I'm having a similar problem on linux. I'm using mime-util-2.1.3.jar and it's detecting some plain text files like .txt, MANIFEST.MF (a text file), and .jsp as [application/octet-stream]. It's working correctly for many otehr file types though. On linux, if I run from a command line "file -i" or "mimetype -M" on these same text files I get the correct mimetypes.

Ok, I think I figured this out for myself (and you)... I looked at the author's unit tests, specifically MimeUtilTest.java and lo and behold, he does this in the unit test setup():
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.ExtensionMimeDetector");
MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.OpendesktopMimeDetector");

I, and you too, was only registering the first one, MagicMimeMimeDetector. Once I added in the other two, it all started working correctly.

Were you able to register MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.OpendesktopMimeDetector");?? I am getting exception while registering it

It worked for me - what exception are you getting? Maybe the APi changed in the 2.5 years since I posted that answer?

Becareful when you use the extension mime detector because it will not check the existence of the target file. It will guess the content type based on the extension given (hence its name).

Respect the OS, so you might try:
  MimeUtil
        .registerMimeDetector(System.getProperty("os.name").startsWith(
              "Windows") ? "eu.medsea.mimeutil.detector.WindowsRegistryMimeDetector"
              : "eu.medsea.mimeutil.detector.OpendesktopMimeDetector");

