Java内置类型检测器有内置,但它自然会错过某些文件类型。
Files.probeContentType(new File(".ttf").toPath());
看起来好像实现允许添加更多 FileTypeDetectors ,但我无法弄清楚如何。
参考:
http://docs.oracle.com/javase/7/docs/api/java/nio/file/spi/FileTypeDetector.html
那么如何才能添加更多要探测的文件?
另一方面,您认为Javas File.proveContentType是否有效?人们不仅需要创建一个Path对象,而且每次都会迭代这些对象。我想顶部需要一个缓存机制。
答案 0 :(得分:1)
要安装自己的FileTypeDetector
,首先要创建自己的实现:
public class MyFileTypeDetector extends java.nio.file.spi.FileTypeDetector {
public String probeContentType(Path path) throws IOException {
return ...
}
}
然后,您将文件/META-INF/services/java.nio.file.spi.FileTypeDetector
添加到包含您的实现的jar中:其内容是您的实现的限定类名:
org.example.MyFileTypeDetector