1

Currently I'm using

MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeType = mimeTypesMap.getContentType(logAttachmentFile.getFilePath());

This works fine with .jpg for example, however most other file types return as application/octet-stream (.xls, .docx, .png) thus making it impossible for me to display the file. Is there a way to determine the proper file type without a third party library? If not, what is the best choice?

Chris R
  • 98
  • 10
  • 1
    Maintan additionally your own mapping, logging unmapped types. Or you could maybe check whether a file type is registered by some application, ugly. – Joop Eggen Mar 17 '17 at 17:57
  • Possible duplicate of [Getting A File's Mime Type In Java](http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java) – beat Mar 17 '17 at 19:08

1 Answers1

0

I believe good option is Apache Tika. It has pretty big range of detected formats: http://tika.apache.org/1.16/formats.html

Also it uses not only the name of the file but also the bytes in it(if the file is very big you can specify just some prefix of the file). And you can customize it pretty easily.

Ruslan Akhundov
  • 2,178
  • 4
  • 20
  • 38