Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...