1 package com.foxinmy.weixin4j.util;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URLConnection;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Map.Entry;
11
12
13
14
15
16
17
18
19
20
21 public class FileUtil {
22
23 private final static Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>();
24
25 static {
26 FILE_TYPE_MAP.put("ffd8ffe000104a464946", "jpg");
27 FILE_TYPE_MAP.put("89504e470d0a1a0a0000", "png");
28 FILE_TYPE_MAP.put("47494638396126026f01", "gif");
29 FILE_TYPE_MAP.put("49492a00227105008037", "tif");
30 FILE_TYPE_MAP.put("424d228c010000000000", "bmp");
31 FILE_TYPE_MAP.put("424d8240090000000000", "bmp");
32 FILE_TYPE_MAP.put("424d8e1b030000000000", "bmp");
33
34 FILE_TYPE_MAP.put("49443303000000002176", "mp3");
35 FILE_TYPE_MAP.put("52494646", "wav");
36 FILE_TYPE_MAP.put("00005741", "wav");
37 FILE_TYPE_MAP.put("2321414d", "amr");
38 FILE_TYPE_MAP.put("520a3c91", "amr");
39
40 FILE_TYPE_MAP.put("2e524d46000000120001", "rmvb");
41 FILE_TYPE_MAP.put("464c5601050000000900", "flv");
42 FILE_TYPE_MAP.put("667479706d70", "mp4");
43 FILE_TYPE_MAP.put("667479706973", "mp4");
44 FILE_TYPE_MAP.put("000001ba210001000180", "mpg");
45 FILE_TYPE_MAP.put("3026b2758e66cf11a6d9", "wmv");
46 FILE_TYPE_MAP.put("52494646d07d60074156", "avi");
47
48 FILE_TYPE_MAP.put("41433130313500000000", "dwg");
49 FILE_TYPE_MAP.put("3c21444f435459504520", "html");
50 FILE_TYPE_MAP.put("3c21646f637479706520", "htm");
51 FILE_TYPE_MAP.put("48544d4c207b0d0a0942", "css");
52 FILE_TYPE_MAP.put("696b2e71623d696b2e71", "js");
53 FILE_TYPE_MAP.put("7b5c727466315c616e73", "rtf");
54 FILE_TYPE_MAP.put("38425053000100000000", "psd");
55 FILE_TYPE_MAP.put("46726f6d3a203d3f6762", "eml");
56 FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "doc");
57 FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "vsd");
58 FILE_TYPE_MAP.put("5374616E64617264204A", "mdb");
59 FILE_TYPE_MAP.put("255044462d312e350d0a", "pdf");
60 FILE_TYPE_MAP.put("4d546864000000060001", "mid");
61 FILE_TYPE_MAP.put("504b0304140000000800", "zip");
62 FILE_TYPE_MAP.put("526172211a0700cf9073", "rar");
63 FILE_TYPE_MAP.put("235468697320636f6e66", "ini");
64 FILE_TYPE_MAP.put("504b03040a0000000000", "jar");
65 FILE_TYPE_MAP.put("4d5a9000030000000400", "exe");
66 FILE_TYPE_MAP.put("3c25402070616765206c", "jsp");
67 FILE_TYPE_MAP.put("4d616e69666573742d56", "mf");
68 FILE_TYPE_MAP.put("3c3f786d6c2076657273", "xml");
69 FILE_TYPE_MAP.put("494e5345525420494e54", "sql");
70 FILE_TYPE_MAP.put("7061636b616765207765", "java");
71 FILE_TYPE_MAP.put("406563686f206f66660d", "bat");
72 FILE_TYPE_MAP.put("1f8b0800000000000000", "gz");
73 FILE_TYPE_MAP.put("6c6f67346a2e726f6f74", "properties");
74 FILE_TYPE_MAP.put("cafebabe0000002e0041", "class");
75 FILE_TYPE_MAP.put("49545346030000006000", "chm");
76 FILE_TYPE_MAP.put("04000000010000001300", "mxp");
77 FILE_TYPE_MAP.put("504b0304140006000800", "docx");
78 FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "wps");
79 FILE_TYPE_MAP.put("6431303a637265617465", "torrent");
80 FILE_TYPE_MAP.put("6D6F6F76", "mov");
81 FILE_TYPE_MAP.put("FF575043", "wpd");
82 FILE_TYPE_MAP.put("CFAD12FEC5FD746F", "dbx");
83 FILE_TYPE_MAP.put("2142444E", "pst");
84 FILE_TYPE_MAP.put("AC9EBD8F", "qdf");
85 FILE_TYPE_MAP.put("E3828596", "pwl");
86 FILE_TYPE_MAP.put("2E7261FD", "ram");
87 }
88
89 private static String bytesToHexString(byte[] src) {
90 StringBuilder stringBuilder = new StringBuilder();
91 for (int i = 0; i < src.length; i++) {
92 int v = src[i] & 0xFF;
93 String hv = Integer.toHexString(v);
94 if (hv.length() < 2) {
95 stringBuilder.append(0);
96 }
97 stringBuilder.append(hv);
98 }
99 return stringBuilder.toString();
100 }
101
102
103
104
105 public static String getFileType(InputStream is) {
106 String fileType = "file";
107 try {
108 byte[] b = new byte[10];
109 int t = is.read(b, 0, b.length);
110 if (t > 0) {
111 String fileCode = bytesToHexString(b).toLowerCase();
112 for (Entry<String, String> entry : FILE_TYPE_MAP.entrySet()) {
113 String key = entry.getKey().toLowerCase();
114 if (key.startsWith(fileCode) || fileCode.startsWith(key) || key.endsWith(fileCode) || fileCode.endsWith(key)) {
115 fileType = entry.getValue();
116 break;
117 }
118 }
119 }
120 } catch (IOException e) {
121 ;
122 } finally {
123 if (is != null) {
124 try {
125 is.close();
126 } catch (IOException ignore) {
127 ;
128 }
129 }
130 }
131 return fileType;
132 }
133
134
135
136
137 public static String getFileExtension(String fileName) {
138 int extensionPos = fileName.lastIndexOf(".");
139 if (extensionPos < 0) {
140 return "";
141 }
142 int lastUnixPos = fileName.lastIndexOf("/");
143 int lastWindowsPos = fileName.lastIndexOf("\\");
144 int lastSeparator = Math.max(lastUnixPos, lastWindowsPos);
145 return lastSeparator > extensionPos ? "" : fileName.substring(extensionPos + 1);
146 }
147
148
149
150
151
152
153 public static boolean deleteFile(String filePath) {
154 File file = new File(filePath);
155 if (!file.exists()) {
156 return true;
157 }
158 if (file.isFile()) {
159 return file.delete();
160 }
161 return false;
162 }
163
164 public static void main(String[] args) throws IOException {
165 InputStream is = new FileInputStream(new File("/Users/jy/Downloads/test.mp4"));
166 System.err.println(getFileType(is));
167 System.err.println(URLConnection.guessContentTypeFromStream(is));
168 }
169 }