This article is after watching the http://blog.csdn.net/chaijunkun/article/details/7046343 practice a bit, for their own deepening the impression to write again.
The jar package is downloaded with Firefox, where it is forgotten. When you write a file download, the prompt box tells the user what type of file is being downloaded.
First of all, the first, but also think the simplest sentence to fix, do not have to guide the jar package, need to import Javax.activation.MimetypesFileTypeMap;
String type = new Mimetypesfiletypemap (). getContentType (name);//name: "Aa.txt"
The second, with the first kind of similar, but the contents of a lot less, many types are not, need to import java.net.FileNameMap;
Import java.net.URLConnection;
fileNameMap fileNameMap = Urlconnection.getfilenamemap (); String type = filenamemap.getcontenttypefor (name);//name: "Aa.txt"
The third kind needs to import the Jmimemagic.jar, needs the import net.sf.jmimemagic.Magic;
Import Net.sf.jmimemagic.MagicMatch;
MagicMatch match = Magic.getmagicmatch (new File ("C:/aa.txt"), false); Note: The input here is the file (must be real), not stringstring type = Match.getmimetype ();
the fourth kind needs to import the Mime-util.jar, needs the import eu.medsea.mimeutil.MimeUtil; import java.util.Collection;
Mimeutil.registermimedetector ("Eu.medsea.mimeutil.detector.MagicMimeMimeDetector"); File F = new file ("C:/aa.txt"); The file must be real collection<?> types = Mimeutil.getmimetypes (f); The return is not a string, it is CollectionSystem.out.println (types);
End it!
Java gets the file MIME type in several ways