Toggle navigation
亻中 阝日
社区
VA伐木累
Proxy
JSON
BASE64
MyGit
登录
注册
×
登录
邮箱
密码
忘记密码?
JAVA读取文件类型,即contentType
•发布于
•作者
liuzy
•857 次浏览
•最后一次编辑是
•来自
技术
- 源码 ```xml
net.sf.jmimemagic
jmimemagic
0.1.5
``` ```java // 效率优先读取文件的contentType。(前三种方法比较快但不一定能读出来,Magic比较慢但基本上能读出来) public static String getFileContentType(File file) { try { String contentType = URLConnection.getFileNameMap().getContentTypeFor(file.getPath()); // 相对耗时8 if (contentType == null || unknown.equals(contentType)) { contentType = Files.probeContentType(file.toPath()); // 相对耗时22 } if (contentType == null || unknown.equals(contentType)) { contentType = new MimetypesFileTypeMap().getContentType(file); // 相对耗时377 } if (contentType == null || unknown.equals(contentType)) { contentType = Magic.getMagicMatch(file, false).getMimeType(); // 相对耗时549 } if (contentType == null) { contentType = unknown; } log.debug("{} => {}", file.getPath(), contentType); return contentType; } catch (Exception e) { log.error("read file type error", e); return null; } } ``` - 测试依据 ```java @Test // 测试4种方法的性能 public void performanceTest() throws Exception { File pdf = new File("/Users/liuzy/Documents/0c814b9a88509c35e74284ba6848bdb1.pdf"); File doc = new File("/Users/liuzy/Documents/1516548713124123123.doc"); File docx = new File("/Users/liuzy/Documents/151654878068871168.docx"); File html = new File("/Users/liuzy/Documents/151797224693567488.html"); File png = new File("/Users/liuzy/Documents/4514ee0ebb9dc7ab5ebd09860264a92b.pdf_106.png"); for (int i = 0; i < 100; i++) { // fuc3(pdf);fuc3(doc);fuc3(docx);fuc3(html);fuc3(png); // 5 10 7 // fuc2(pdf);fuc2(doc);fuc2(docx);fuc2(html);fuc2(png); // 22 21 23 // fuc1(pdf);fuc1(doc);fuc1(docx);fuc1(html);fuc1(png); // 377 370 383 fuc4(pdf);fuc4(doc);fuc4(docx);fuc4(html);fuc4(png); // 520 600 525 } } private void fuc3(File file) throws Exception { URLConnection.getFileNameMap().getContentTypeFor(file.getPath()); } private void fuc2(File file) throws Exception { Files.probeContentType(file.toPath()); } private void fuc1(File file) { new MimetypesFileTypeMap().getContentType(file); } private void fuc4(File file) throws Exception { Magic.getMagicMatch(file, false).getMimeType(); } ```
0 回复
作者
liuzy
积分: 841
“ 黑眼圈圈男 ”
无人回复话题
SonarQube 9.4 + PostgreSQL
shell倒计时
日常网络巧技
使用ssh创建socks5代理服务
NodeJS集群demo
作者其他话题
SonarQube 9.4 + PostgreSQL
shell倒计时
日常网络巧技
使用ssh创建socks5代理服务
NodeJS集群demo
回到顶部
友情链接:
JFinal
©2015 Powered by
jfinalbbs
沪ICP备15012258号