Java gets the file MIME type in several ways

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.