ADDITIONAL SYSTEM INFORMATION :
Java 11 (on CentOS Linux release 7.9.2009 (Core) with openjdk 11.0.14.1 2022-02-08 LTS).
Also occurs in Java 18.0.1.1 (JShell Version 18.0.1.1 on Docker openjdk:18.0.1).
A DESCRIPTION OF THE PROBLEM :
Disclaimer: https://bugs.openjdk.java.net/browse/JDK-8283622 was closed as "Cannot Reproduce" without properly investigating the issue, so I have to file a new bug.
On Linux, the java.nio.file.Files.probeContentType method unexpectedly returns null if the filename contains a hash mark "#".
The problem is caused by sun.net.www.MimeTable.findByFileName(String), which is used by the default file type detector (sun.nio.fs.DefaultFileTypeDetector) as fallback, and which tries to determine the extension for filenames and URLs alike and omits characters after a supposed fragment identifier "#".
However, the implementation neglects that "#" is a valid character for most file systems (especially under Linux).
Consequently, the content type for a file named "b.png" yields "image/png", but for "a#b.png" yields null.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Reproduction of the bug might depend on the Linux distribution and which capabilities for content-type probing are available.
It can be reproduced via the Docker by running "docker run -it openjdk:18.0.1":
PS C:\test> docker run -it openjdk:18.0.1
Unable to find image 'openjdk:18.0.1' locally
18.0.1: Pulling from library/openjdk
90a00d516db1: Pull complete
06fc60984518: Pull complete
f4f239d0320f: Pull complete
Digest: sha256:aeea19efe712520a7e58d24bc608b2a8470406a238d2a8168838b4677ca7f651
Status: Downloaded newer image for openjdk:18.0.1
May 24, 2022 5:37:21 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
| Welcome to JShell -- Version 18.0.1.1
| For an introduction type: /help intro
jshell>
jshell> java.nio.file.Files.probeContentType(java.nio.file.Paths.get("b.png"))
$1 ==> "image/png"
jshell> java.nio.file.Files.probeContentType(java.nio.file.Paths.get("a#b.png"))
$2 ==> null
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Content type based on filename extension (e.g., "image/png")
---------- BEGIN SOURCE ----------
Assert.assertEquals("image/png", Files.probeContentType(Paths.get("b.png"))); // Passes
Assert.assertEquals("image/png", Files.probeContentType(Paths.get("a#b.png"))); // Fails
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Install a custom FileTypeDetector.
FREQUENCY : always
Java 11 (on CentOS Linux release 7.9.2009 (Core) with openjdk 11.0.14.1 2022-02-08 LTS).
Also occurs in Java 18.0.1.1 (JShell Version 18.0.1.1 on Docker openjdk:18.0.1).
A DESCRIPTION OF THE PROBLEM :
Disclaimer: https://bugs.openjdk.java.net/browse/JDK-8283622 was closed as "Cannot Reproduce" without properly investigating the issue, so I have to file a new bug.
On Linux, the java.nio.file.Files.probeContentType method unexpectedly returns null if the filename contains a hash mark "#".
The problem is caused by sun.net.www.MimeTable.findByFileName(String), which is used by the default file type detector (sun.nio.fs.DefaultFileTypeDetector) as fallback, and which tries to determine the extension for filenames and URLs alike and omits characters after a supposed fragment identifier "#".
However, the implementation neglects that "#" is a valid character for most file systems (especially under Linux).
Consequently, the content type for a file named "b.png" yields "image/png", but for "a#b.png" yields null.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Reproduction of the bug might depend on the Linux distribution and which capabilities for content-type probing are available.
It can be reproduced via the Docker by running "docker run -it openjdk:18.0.1":
PS C:\test> docker run -it openjdk:18.0.1
Unable to find image 'openjdk:18.0.1' locally
18.0.1: Pulling from library/openjdk
90a00d516db1: Pull complete
06fc60984518: Pull complete
f4f239d0320f: Pull complete
Digest: sha256:aeea19efe712520a7e58d24bc608b2a8470406a238d2a8168838b4677ca7f651
Status: Downloaded newer image for openjdk:18.0.1
May 24, 2022 5:37:21 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
| Welcome to JShell -- Version 18.0.1.1
| For an introduction type: /help intro
jshell>
jshell> java.nio.file.Files.probeContentType(java.nio.file.Paths.get("b.png"))
$1 ==> "image/png"
jshell> java.nio.file.Files.probeContentType(java.nio.file.Paths.get("a#b.png"))
$2 ==> null
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Content type based on filename extension (e.g., "image/png")
---------- BEGIN SOURCE ----------
Assert.assertEquals("image/png", Files.probeContentType(Paths.get("b.png"))); // Passes
Assert.assertEquals("image/png", Files.probeContentType(Paths.get("a#b.png"))); // Fails
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Install a custom FileTypeDetector.
FREQUENCY : always
- relates to
-
JDK-8283622 Files.probeContentType returns null if filename contains hash mark
-
- Closed
-