0

It seems that Files.probeContentType() returns null for a file (where the MIME type is not indicated by its file name) on OpenJDK 11, while it was working on OpenJDK 8 (on Linux x64). What is the reason for that?

Note: It may work for files where the type is indicated by its name, e.g., "test.pdf" will produce "application/pdf", but renaming the file to "test" will produce null.

pxcv7r
  • 478
  • 5
  • 14
  • **Unable to reproduce.** I ran `Files.probeContentType(Paths.get("test.pdf"))` on Window using Oracle 1.8.0_181, OpenJDK 11.0.2, and AdoptOpenJDK 15.0.1+9, and they all returned `application/pdf`. – Andreas Jan 30 '21 at 09:47
  • @Andreas, this is platform-specific. On Windows, there is `RegistryFileTypeDetector` which detects the type based on the file name extension being mapped to an application in the Windows registry. – pxcv7r Jan 30 '21 at 09:53
  • @Andreas, on Windows you can reproduce this by renaming your PDF file to "test" without the extension. The (only) available detector does not inspect the contents, but will fail to lookup the (non-existing) extension in the registry. – pxcv7r Jan 30 '21 at 10:02

2 Answers2

2

File type detection is largely platform-specific and was not considered reliable. For instance, GnomeFileTypeDetector was available in JDK 8, but only working on Linux systems.

The OpenJDK developers have decided to remove both GnomeFileTypeDetector and MagicFileTypeDetector from the JDK (already in JDK 9), see OpenJDK bug tracker and here.

Available detectors MimeTypesFileTypeDetector (on Linux) or RegistryFileTypeDetector on Windows simply implement a mapping from file name extensions to MIME types, but do not inspect the actual contents of files.

(Note: all detector implementations are found (or not) in package sun.nio.fs.)

pxcv7r
  • 478
  • 5
  • 14
0

I have the same problem on docker container with Ubuntu 20.04. Normally a container is very small and got fiew very specific tools. I solved installing libmagic-mgc and libmagic1 package by apt command, which contains the famous command "file" used to inspect file MimeType.

fante76
  • 55
  • 6