4

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

Aksel Willgert
  • 11,367
  • 5
  • 53
  • 74
Md. Reazul Karim
  • 645
  • 2
  • 7
  • 15
  • Did you see that http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java ? – Georgy Gobozov Dec 08 '12 at 10:28
  • 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. – Md. Reazul Karim Dec 08 '12 at 18:32
  • 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. – Brian Pipa Dec 11 '12 at 16:08

2 Answers2

3

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.

Brian Pipa
  • 808
  • 9
  • 23
  • Were you able to register `MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.OpendesktopMimeDetector");`?? I am getting exception while registering it – kittu Jun 19 '15 at 04:54
  • It worked for me - what exception are you getting? Maybe the APi changed in the 2.5 years since I posted that answer? – Brian Pipa Jun 25 '15 at 18:18
  • 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). – TheRealChx101 Apr 27 '20 at 15:27
2

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");
HGO
  • 21
  • 3