Reputation: 655
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
Upvotes: 4
Views: 5060
Reputation: 21
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");
Upvotes: 2
Reputation: 806
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.
Upvotes: 3