22

我在 mac 上使用 java se7,oracle 预览版。

我的问题是“Files.probeContentType”返回null ...是否有可能是由于se7 for mac的早期状态?我的代码:

if(directory == null) return;
String content = null;
try {
    content = Files.probeContentType(directory.toPath());
} catch (IOException e) {
    JOptionPane.showMessageDialog(main, e.toString());
    return;
}
if(content == null)
{
    return;
}
else if(content.contains("image"))
{
    main.pctviewer.setImage(directory);
}

该文件的名称是:

“/Users/admin/Desktop/temp/q12/formulare/Bildschirmfoto 2012-09-11 um 17.57.59.png”

如果我将鼠标悬停在文件“文件路径= Unis-path(id:145)”上方,则在eclipse中的调试模式下为红色

4

3 回答 3

12

我再次向 oracle 报告了这个错误,希望他们能够支持 jdk8 解决方案(我不抱太大希望,但你永远不知道)。

同时,您可以使用我自己在https://github.com/jeantil/jdk7-mimeutils上提供的 FileTypeDetector 反向移植,将 maven 项目打包到一个 jar 中,该 jar 可以添加到您的类路径中以启用 mime 类型检测。我还提供了一个 mime.types 文件放入您的主文件夹中,以便检测正常工作。我从某个版本的 apache 中提取了 mime.types 文件,所以它非常完整。

于 2013-08-22T15:57:34.730 回答
4

我发现 FileTypeDetector 在 OS X 上有错误:http ://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7133484

显然这将在 Java 8 中修复。

于 2013-08-21T06:22:45.977 回答
1

使用以下方法在 Java 8 中获取 mime 类型的文件:

String location = "/Users/user/Desktop/leaf.jpg";
File file = new File(location);
Path source = Paths.get(location);
MimetypesFileTypeMap m = new MimetypesFileTypeMap(source.toString());
System.out.println( m.getContentType(file) );

上述代码的输出是:'image/jpg'

于 2019-09-13T05:24:35.127 回答