我需要检查MIME类型的currentFile.如果结果是成功并且文件具有MIME类型返回true.如果没有检查成功则返回false.
有了这个目标,我使用
JMimeMagic.
我尝试这样做
according this post
此代码的输出是 – net.sf.jmimemagic.MagicMatchNotFoundException
您需要JDK 7 – 以这种方式将File更改为byte [](Files.readAllBytes(path)).
码:
class ProbeContentTypeCheker implements Checker {
@Override
public boolean check(File currentFile) {
String mimeType = null;
try {
Path path = Paths.get(currentFile.getAbsolutePath());
byte[] data = Files.readAllBytes(path);
MagicMatch match = Magic.getMagicMatch(data);
mimeType = match.getMimeType();
} catch (MagicParseException | MagicMatchNotFoundException
| MagicException | IOException e) {
e.printStackTrace();
}
if (null != mimeType) {
return true