简体   繁体   中英

why does Files.probeContentType return null

i am using java se7 on mac, the oracle preview.

My problem is that "Files.probeContentType" returns null...is it possible that its due to the early status of se7 for mac? My code:

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);
}

the name of the file is:

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

and in debug mode in eclipse if i hover above File "file path = Unis-path(id:145)" is red

I have reported the bug to oracle again, hoping they will backport the jdk8 solution (I don't have much hope but you never know).

In the meantime you can use my own backport of the FileTypeDetector available at https://github.com/jeantil/jdk7-mimeutils the maven project packages to a jar which can be added to your classpath to enable mime type detection. I also provide a mime.types file to put in your home folder for the detection to work correctly. I extracted the mime.types file from some version of apache so it's pretty complete.

I found that the FileTypeDetector is buggy on OS X: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7133484

Apparently this will be fixed in Java 8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM