2

I am trying to read file headers using java, I want to get file type(image, audio whatever). I have tried many examples but nothing seems to work. I tried preon and no luck, can some one tell me how to read file headers using preon or some other Java apis thanks...

Gary
  • 13,303
  • 18
  • 49
  • 71
Eric
  • 1,171
  • 2
  • 14
  • 27
  • 1
    See this duplicate to aid achieve that. http://stackoverflow.com/questions/8191720/reading-the-header-of-a-file-in-java – Bitmap May 08 '12 at 09:39
  • Check also this one: http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java – Guillaume Polet May 08 '12 at 10:16
  • Define "nothing seems to work" and "no luck". Specifically, what results are you expecting and what results are you actually getting? and with what code? against what files? – user207421 May 08 '12 at 11:06
  • @Eric I know this comment is late, but please accept an answer instead of editing "closed" into the title. – Gary Sep 30 '14 at 22:39

2 Answers2

4
 File f = new File(filepath);
 System.out.println("Mime Type of " + f.getName() + " is " +  
 new MimetypesFileTypeMap().getContentType(f));
Dilip Godhani
  • 2,065
  • 3
  • 18
  • 33
  • 1
    it only works if file has an extension, I am trying to read unknown file types (like temp, dump files without any .extension) – Eric May 08 '12 at 10:12
0

Use mime-util's MagicMimeMimeDetector:

detector.getMimeTypes(file)
Wilfred Springer
  • 10,869
  • 4
  • 55
  • 69