Content Table

# 自定义 Content Type Prober

• 在 A 网站点击 a.pdf，浏览器自动下载 a.pdf
• 在 B 网站点击 b.pdf，浏览器直接打开 b.pdf

This method uses the installed FileTypeDetector implementations to probe the given file to determine its content type. Each file type detector’s probeContentType is invoked, in turn, to probe the file type. If the file is recognized then the content type is returned. If the file is not recognized by any of the installed file type detectors then a system-default file type detector is invoked to guess the content type.

A given invocation of the Java virtual machine maintains a system-wide list of file type detectors. Installed file type detectors are loaded using the service-provider loading facility defined by the ServiceLoader class. Installed file type detectors are loaded using the system class loader. If the system class loader cannot be found then the extension class loader is used; If the extension class loader cannot be found then the bootstrap class loader is used. File type detectors are typically installed by placing them in a JAR file on the application class path or in the extension directory, the JAR file contains a provider-configuration file named java.nio.file.spi.FileTypeDetector in the resource directory META-INF/services, and the file lists one or more fully-qualified names of concrete subclass of FileTypeDetector that have a zero argument constructor. If the process of locating or instantiating the installed file type detectors fails then an unspecified error is thrown. The ordering that installed providers are located is implementation specific.

• main/java/xtuer/ContentTypeProber 是 FileTypeDetector 接口的实现类，核心代码
• META-INF/services/java.nio.file.spi.FileTypeDetector 用于 SPI 的配置
• config/content-type.properties 是文件类型与 Content-Type 的映射关系
• ContentTypeProberTest 是测试类