Java get file MIME type __mime

Source: Internet
Author: User

We know that there are often content-type headers in the HTTP request header, which is critical when Java uses post to make a request, and we can't ignore it. In the last few days of work, because I have been in contact with post upload files to send requests, so in this encounter a lot of problems, now we first look at how to get a file MIME type. So let's explain the MIME concept first.

MIME (Multipurpose Internet Mail Extensions) Multipurpose Internet Mail Extension type. Is the type of file that sets an extension to open with an application, and when the file is accessed, the browser automatically opens with the specified application. Used to specify some client-defined file names and how some media files are opened. This is my Baidu encyclopedia excerpt on a section of the introduction, specific you can go to Baidu Encyclopedia access.

OK, let's take a look at how to use Java to get the Content-type value of the file, and then follow my custom to code first:

Import java.io.IOException;
Import Java.nio.file.Files;
Import Java.nio.file.Path;
Import java.nio.file.Paths;
/**
 * Test class
 * @author ZY
 * * *
/public class Mime_demo {public
	static void Main (string[] args) throw s Exception {
		System.out.println (getContentType ("f:/test.png"));
		System.out.println (getContentType ("F:/2.doc"));
		System.out.println (getContentType ("f:/2.csv"));
		System.out.println (getContentType ("F:/liveupdate.exe"));
		System.out.println (getContentType ("F:/1.txt"));
		System.out.println (getContentType ("f:/demo.jpg"));
	}
	
	public static string getContentType (string filename) {
		string type = null;
		Path PATH = paths.get (filename);
		try {
			type = files.probecontenttype (path);
		} catch (IOException e) {
			e.printstacktrace ();
		}
		return type;
	}
}

The execution of this code results in the following:

Image/png
application/msword
application/vnd.ms-excel
application/x-msdownload
text/plain
Image/jpeg
It's good to get the Content-type value of the file, but here's a wake-up call, because the java.nio.file.* package is in jdk1.7, so it's not available for a lower version of the JDK. Remind again, need to use jdk1.7 and above.





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.