Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the <code>mime type</code> instead of trying to guess by the file extensions, that may be missing in some cases. Here are the options to establish the <code>mime type</code> of a file:</p> <ol> <li><p>Using <code>javax.activation.MimetypesFileTypeMap</code>, like:</p> <pre><code>System.out.println("Mime Type of " + f.getName() + " is " + new MimetypesFileTypeMap().getContentType(f)); </code></pre></li> <li><p>Using <code>java.net.URL</code></p> <pre><code>URL u = new URL(fileUrl); URLConnection uc = u.openConnection(); type = uc.getContentType(); </code></pre></li> <li><p>Using <code>Apache Tika</code></p> <pre><code>ContentHandler contenthandler = new BodyContentHandler(); Metadata metadata = new Metadata(); metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName()); Parser parser = new AutoDetectParser(); // OOXMLParser parser = new OOXMLParser(); parser.parse(is, contenthandler, metadata); System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE)); System.out.println("Title: " + metadata.get(Metadata.TITLE)); System.out.println("Author: " + metadata.get(Metadata.AUTHOR)); System.out.println("content: " + contenthandler.toString()); </code></pre></li> <li><p>Using <code>JMimeMagic</code></p> <pre><code>MagicMatch match = parser.getMagicMatch(f); System.out.println(match.getMimeType()) ; </code></pre></li> <li><p>Using <code>mime-util</code></p> <pre><code>Collection&lt;?&gt; mimeTypes = MimeUtil.getMimeTypes(f); </code></pre></li> <li><p>Using <a href="http://droid.sourceforge.net/wiki/index.php/Introduction" rel="noreferrer">DROID</a></p> <pre><code>Droid (Digital Record Object Identification) is a software tool to perform automated batch identification of file formats. </code></pre></li> <li><p><a href="http://aperture.wiki.sourceforge.net/Overview" rel="noreferrer">Aperture</a> framework</p> <pre><code>Aperture is an open source library and framework for crawling and indexing information sources such as file systems, websites and mail boxes. </code></pre></li> </ol> <p>See <a href="http://www.rgagnon.com/javadetails/java-0487.html" rel="noreferrer">Get the Mime Type from a File</a> for more details for each of the above options. </p> <p>In this case the easiest way is to use the first solution, <code>javax.activation.MimetypesFileTypeMap</code>, like:</p> <pre><code>MimetypesFileTypeMap mtft = new MimetypesFileTypeMap(); String mimeType = mtft.getContentType(ze.getName()); System.out.println(ze.getName()+" type: "+ mimeType); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload