Techfee

About How to Get Mime Types Using Java

| Comments

1. using javax.activation.MimetypesFileTypeMap

1
2
 File f = new File("test.docx");
 String mimeType = new MimetypesFileTypeMap().getContentType(f);
1
2
3
4
5
6
result:
Mime Type of .DS_Store is application/octet-stream
Mime Type of 444.docx is application/octet-stream
Mime Type of f.jar is application/octet-stream
Mime Type of Non-Disclosure Agreement.docx is application/octet-stream
Mime Type of W9 Form.pddf is application/octet-stream

2. Write yourself

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 private static final int PREFIX_COUNT = 20;

  // PDF tag
  private static final String PDF_TAG = "25 50 44 46";
  // Microsoft
  private static final String DOC1_TAG = "D0 CF 11 E0 A1 B1 1A E1";
  // Word Document
  private static final String DOC2_TAG = "EC A5 C1 00";
  // Perfect Office Document
  private static final String DOC3_TAG = "CF 11 E0 A1 B1 1A E1 00";
  // Deskmate
  private static final String DOC4_TAG = "0D 44 4F 43";
  // DOCX
  private static final String DOCX_TAG = "50 4B 3 4 14 0 6 0";
  // RTF
  private static final String RTF_TAG = "7B 5C 72 74 66 31";

  public static string getMimeType(String filePath) {

      String rawFileName = filePath;

      File raw_file = new File(rawFileName);

      byte[] raw_result = new byte[PREFIX_COUNT];

      try {
          InputStream raw_input = null;
          try {

              raw_input = new BufferedInputStream(new FileInputStream(raw_file));

              raw_input.read(raw_result, 0, PREFIX_COUNT);

          } finally {

              raw_input.close();
          }
      } catch (FileNotFoundException ex) {
      }

      catch (IOException ex) {

          e.printStackTrace();

      }

      StringBuffer byteTemp = new StringBuffer("");

      for (int j = 0; j < PREFIX_COUNT; j++) {

          String temp = Integer.toHexString(raw_result[j]);

          if (temp.length() > 2) {
              byteTemp.append(temp.substring(temp.length() - 2));
          } else {
              byteTemp.append(temp);
          }

          byteTemp.append(" ");

      }

      int doctype = findDocType(byteTemp.toString().toUpperCase());

      switch (doctype) {

      case 1:
          return "filetype:doc";
          
      case 2:
          return "filetype:docx";

      case 3:
          return "filetype:rtf";

      default:
          return "filetype:unknow";

      }

  }

  private static int findDocType(String prefix) {

      // System.out.println(prefix);

      if (prefix.startsWith(DOCX_TAG)) {
          return 2;
      }
      if (prefix.startsWith(DOC1_TAG)) {
          return 1;
      }
      if (prefix.startsWith(RTF_TAG)) {
          return 3;
      }

      return 0;

  }

Comments