Java获取文件类型Mime Type的各种方法
编程技术  /  houtizong 发布于 3年前   64
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" }}
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 }}
import java.net.FileNameMap;import java.net.URLConnection;public class FileUtils { public static String getMimeType(String fileUrl) throws java.io.IOException { FileNameMap fileNameMap = URLConnection.getFileNameMap(); String type = fileNameMap.getContentTypeFor(fileUrl); return type; } public static void main(String args[]) throws Exception { System.out.println(FileUtils.getMimeType("file://c:/temp/test.TXT")); // output : text/plain } }
import java.io.File;import java.io.FileInputStream;import org.apache.tika.metadata.Metadata;import org.apache.tika.parser.AutoDetectParser;import org.apache.tika.parser.Parser;import org.apache.tika.sax.BodyContentHandler;import org.xml.sax.ContentHandler;public class Main { public static void main(String args[]) throws Exception { FileInputStream is = null; try { File f = new File("C:/Temp/mime/test.docx"); is = new FileInputStream(f); ContentHandler contenthandler = new BodyContentHandler(); Metadata metadata = new Metadata(); metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName()); Parser parser = new AutoDetectParser(); // OOXMLParser parser = new OOXMLParser(); parser.parse(is, contenthandler, metadata); System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE)); System.out.println("Title: " + metadata.get(Metadata.TITLE)); System.out.println("Author: " + metadata.get(Metadata.AUTHOR)); System.out.println("content: " + contenthandler.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) is.close(); } }}
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()) ;
import eu.medsea.mimeutil.MimeUtil;public class Main { public static void main(String[] args) { MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector"); File f = new File ("c:/temp/mime/test.doc"); Collection<?> mimeTypes = MimeUtil.getMimeTypes(f); System.out.println(mimeTypes); // output : application/msword }}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接