java - How to get the MIME Type of a .MSG file? -


i have tried these ways of finding mime type of file...

path source = paths                 .get("c://users/akash/desktop/fw internal release of mstclient-server5.02.04_24.msg");         system.out.println(files.probecontenttype(source)); 

the above code returns null...
, if use tika api apache mime type gives text/plain...

but want result application/vnd.ms-outlook

update

i used mime-util.jar follows code...

mimeutil2 mimeutil = new mimeutil2();         mimeutil.registermimedetector("eu.medsea.mimeutil.detector.magicmimemimedetector");         randomaccessfile file1 = new randomaccessfile(                 "c://users/akash/desktop/fw internal release of mstclient-server5.02.04_24.msg",                 "r");         system.out.println(file1.length());         byte[] file = new byte[624128];         file1.read(file, 0, 624128);         string mimetype = mimeutil2.getmostspecificmimetype(mimeutil.getmimetypes(file)).tostring(); 

this gives me output application/msword

update:

tika api out of scope large include in project...

so how can find mime type?

i tried of possible ways , using tika gives result expected, don't see code used cannot double check it.

i tried different ways, not in code snippet:

  1. java 7 files.probecontenttype(path)
  2. urlconnection mime detection file name , content type guessing
  3. jdk 6 jaf api javax.activation.mimetypesfiletypemap
  4. mimeutil available subclass of mimedetector found
  5. apache tika
  6. apache poi scratchpad

here test class:

import java.io.bufferedinputstream; import java.io.file; import java.io.fileinputstream; import java.io.inputstream; import java.net.urlconnection; import java.util.collection;  import javax.activation.mimetypesfiletypemap;  import org.apache.tika.detect.detector; import org.apache.tika.metadata.metadata; import org.apache.tika.mime.mediatype; import org.apache.tika.parser.autodetectparser;  import eu.medsea.mimeutil.mimeutil;  public class findmime {      public static void main(string[] args) {         file file = new file("c:\\users\\qwerty\\desktop\\test.msg");          system.out.println("urlconnectionguess " + urlconnectionguess(file));          system.out.println("filecontentguess " + filecontentguess(file));          mimetypesfiletypemap mimetypesmap = new mimetypesfiletypemap();          system.out.println("mimetypesmap.getcontenttype " + mimetypesmap.getcontenttype(file));          system.out.println("mimeutils " + mimeutils(file));          system.out.println("tika " + tika(file));      }      private static string mimeutils(file file) {         try {             mimeutil.registermimedetector("eu.medsea.mimeutil.detector.magicmimemimedetector");             mimeutil.registermimedetector("eu.medsea.mimeutil.detector.extensionmimedetector"); //          mimeutil.registermimedetector("eu.medsea.mimeutil.detector.opendesktopmimedetector");             mimeutil.registermimedetector("eu.medsea.mimeutil.detector.windowsregistrymimedetector"); //          mimeutil.registermimedetector("eu.medsea.mimeutil.detector.textmimedetector");             inputstream = new bufferedinputstream(new fileinputstream(file));             collection<?> mimetypes = mimeutil.getmimetypes(is);             return mimetypes.tostring();         } catch (exception e) {             // todo: handle exception         }         return null;     }      private static string tika(file file) {         try {             inputstream = new bufferedinputstream(new fileinputstream(file));             autodetectparser parser = new autodetectparser();             detector detector = parser.getdetector();             metadata md = new metadata();             md.add(metadata.resource_name_key, "test.msg");             mediatype mediatype = detector.detect(is, md);             return mediatype.tostring();         } catch (exception e) {             // todo: handle exception         }         return null;     }      private static string urlconnectionguess(file file) {         string mimetype = urlconnection.guesscontenttypefromname(file.getname());         return mimetype;     }      private static string filecontentguess(file file) {         try {             inputstream = new bufferedinputstream(new fileinputstream(file));             return urlconnection.guesscontenttypefromstream(is);         } catch (exception e) {             e.printstacktrace();             return null;         }     }  } 

and output:

urlconnectionguess null filecontentguess null mimetypesmap.getcontenttype application/octet-stream mimeutils application/msword,application/x-hwp tika application/vnd.ms-outlook 

updated added method test other ways tika:

private static void tikamore(file file) {     tika defaulttika = new tika();     tika mimetika = new tika(new mimetypes());     tika typetika = new tika(new typedetector());     try {         system.out.println(defaulttika.detect(file));         system.out.println(mimetika.detect(file));         system.out.println(typetika.detect(file));     } catch (exception e) {         // todo: handle exception     } } 

tested msg file without extension:

application/vnd.ms-outlook application/octet-stream application/octet-stream 

tested txt file renamed msg:

text/plain text/plain application/octet-stream 

seems simple way using empty constructor reliable in case.

update can make own checker using apache poi scratchpad, example simple implementation mime of message or null if file not in proper format (usually org.apache.poi.poifs.filesystem.notole2fileexception: invalid header signature):

import org.apache.poi.hsmf.mapimessage;  public class poimsgmime {      public string getmessagemime(string filename) {         try {             new mapimessage(filename);             return "application/vnd.ms-outlook";         } catch (exception e) {             return null;         }     } } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -