Get the Mime Type from a File
来源:互联网 发布:2017网络电视剧排行榜 编辑:程序博客网 时间:2024/06/06 12:21
Using javax.activation.MimetypesFileTypeMap
activation.jar is required, it can be downloaded from http://java.sun.com/products/javabeans/glasgow/jaf.html.The MimetypesFileMap class is used to map a File to a Mime Type. Mime types supported are defined in a ressource file inside the activation.jar.
import javax.activation.MimetypesFileTypeMap;import java.io.File;class GetMimeType { public static void main(String args[]) { File f = new File("gumby.gif"); System.out.println("Mime Type of " + f.getName() + " is " + new MimetypesFileTypeMap().getContentType(f)); // expected output : // "Mime Type of gumby.gif is image/gif" }}
The MimetypesFileTypeMap looks in various places in the user's system for MIME types file entries. When requests are made to search for MIME types in the MimetypesFileTypeMap, it searches MIME types files in the following order:
- Programmatically added entries to the MimetypesFileTypeMap instance.
- The file .mime.types in the user's home directory.
- The file <java.home>/lib/mime.types.
- The file or resources named META-INF/mime.types.
- The file or resource named META-INF/mimetypes.default (usually found only in the activation.jar file).
Using java.net.URL
Warning : this method is very slow!.Like the above method a match is done with the extension. The mapping between the extension and the mime-type is defined in the file [jre_home]/lib/content-types.properties
import java.net.*;public class FileUtils{ public static String getMimeType(String fileUrl) throws java.io.IOException, MalformedURLException { String type = null; URL u = new URL(fileUrl); URLConnection uc = null; uc = u.openConnection(); type = uc.getContentType(); return type; } public static void main(String args[]) throws Exception { System.out.println(FileUtils.getMimeType("file://c:/temp/test.TXT")); // output : text/plain }}
Using JMimeMagic
Checking the file extension is not a very strong way to determine the file type. A more robust solution is possible with the JMimeMagic library. JMimeMagic is a Java library (LGLP licence) that retrieves file and stream mime types by checking magic headers.// snippet for JMimeMagic lib// http://sourceforge.net/projects/jmimemagic/Magic parser = new Magic() ;// getMagicMatch accepts Files or byte[], // which is nice if you want to test streamsMagicMatch match = parser.getMagicMatch(new File("gumby.gif"));System.out.println(match.getMimeType()) ;
Using mime-util
Another tool is mime-util. This tool can detect using the file extension or the magic header technique.// snippet for mime-util lib// http://sourceforge.net/projects/mime-utilpublic static final String UNKNOWN_MIME_TYPE="application/x-unknown-mime-type";...String mimeType = MimeUtil.getMagicMimeType(file);if(mimeType == null) mimeType = UNKNOWN_MIME_TYPE;
Using Droid
DROID (Digital Record Object Identification) is a software tool to perform automated batch identification of file formats.DROID uses internal and external signatures to identify and report the specific file format versions of digital files. These signatures are stored in an XML signature file, generated from information recorded in the PRONOM technical registry. New and updated signatures are regularly added to PRONOM, and DROID can be configured to automatically download updated signature files from the PRONOM website via web services.
It can be invoked from two interfaces, a Java Swing GUI or a command line interface.
http://droid.sourceforge.net/wiki/index.php/Introduction
Aperture framework
Aperture is an open source library and framework for crawling and indexing information sources such as file systems, websites and mail boxes.The Aperture code consists of a number of related but independently usable parts:
- Crawling of information sources: file systems, websites, mail boxes
- MIME type identification
- Full-text and metadata extraction of various file formats
- Opening of crawled resources
http://aperture.wiki.sourceforge.net/Overview
- Get the Mime Type from a File
- Get the Mime Type from a File
- jQuery: Get filename from input [type='file']
- wprintf, fork, fwide, get FILE* from a file descriptor
- get the owner of a file
- AddType requires at least two arguments, a mime type followed by one or more file extensions
- AddType requires at least two arguments, a mime type followed by one or more file extensions
- AddType requires at least two arguments, a mime type followed by one or more file extensions
- Get value from properties file outside a jar using Spring
- get file's name from a folder using Java
- How to get file name when user select a file via <input type=“file” />?
- Testing the Type of a File in Unix
- String cannot be resolved to a type - The method getContextPath() from the type HttpServletRequest r
- To get the "month" from a DATE datatype
- redis JedisConnectionException: Could not get a resource from the pool
- Could not get a resource from the pool
- Could not get a resource from the pool 错误分析
- Could not get a resource from the pool
- WSDL2Java简述
- OpenOffice_org系列讲座(一)
- MTOS 4.2rc3中文化包
- VC++/MFC学习笔记(一)
- 如何用ASP生成静态Html文件
- Get the Mime Type from a File
- 算法
- OpenOffice_org系列讲座(二)
- 多层头,多列头,合并列头,双列头
- BBC削弱对Microformats的支持
- 将8个BOOL值放入一个字节中
- 成绩管理算法
- LINQ学习笔记三-----基本查询操作
- ARM学习相关网站