Get the Mime type of the file in Java

I just want to know how most people get MIME types from Java files? So far, I have tried two JMimeMagic: JMimeMagic and mime util.

The first gives me memory exceptions, and the second does not properly close its stream. I just want to know if other people have the methods / libraries they use and work with?

#1 building

I tried several methods, including @Joshua Fox said the first way. But some can't recognize frequent impersonation types, such as PDF files, while others can't be trusted by forged files (I'm trying to change the extension to TIF's RAR file). Solutions I found (e.g @Joshua Fox) is to use MimeUtil2 , as follows:

MimeUtil2 mimeUtil = new MimeUtil2();
mimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
String mimeType = MimeUtil2.getMostSpecificMimeType(mimeUtil.getMimeTypes(file)).toString();

#2 building

If you are an Android Developer, you can use the utility class android.webkit.mimetypmap, which maps MIME types to file extensions and vice versa.

The following code snippets may help you.

private static String getMimeType(String fileUrl) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}

#3 building

Apache Tika stay tika-core medium Provides mime type detection based on magic tags in stream prefixes. Tika core does not get other dependencies, which makes it different from the currently unmaintained Mime Type Detection Utility Just as light.

A simple code example (Java 7) uses the variables theInputStream and theFileName

try (InputStream is = theInputStream;
        BufferedInputStream bis = new BufferedInputStream(is);) {
    AutoDetectParser parser = new AutoDetectParser();
    Detector detector = parser.getDetector();
    Metadata md = new Metadata();
    md.add(Metadata.RESOURCE_NAME_KEY, theFileName);
    MediaType mediaType = detector.detect(bis, md);
    return mediaType.toString();
}

Please note that MediaType.detect (...) cannot be used directly( TIKA-1120 ) For more tips, please Visit https://tika.apache.org/0.10/detection.html .

#4 building

I just want to know how most people get mime types from Java files?

I've released it SimpleMagic Java package that allows content types (MIME types) to be determined from files and byte arrays. It is designed to read and run Unix file (1) command magic files, which are part of most Unix OS configurations.

I tried to use the Apache pickup but ton of dependency. This is a huge URLConnection that does not use the bytes of the file. The mimetypes filetype map only looks at the file name.

With SimpleMagic, you can do the following:

// create a magic utility using the internal magic file
ContentInfoUtil util = new ContentInfoUtil();
// if you want to use a different config file(s), you can load them by hand:
// ContentInfoUtil util = new ContentInfoUtil("/etc/magic");
...
ContentInfo info = util.findMatch("/tmp/upload.tmp");
// or
ContentInfo info = util.findMatch(inputStream);
// or
ContentInfo info = util.findMatch(contentByteArray);

// null if no match
if (info != null) {
   String mimeType = info.getMimeType();
}

#5 building

If you stick to Java 5-6, then from Servo open source products This utility class.

You only need this function

public static String getContentType(byte[] data, String name)

It probes the first few bytes of content and returns the content type based on that content rather than the file extension.

Tags: Java Apache Android Unix

Posted on Tue, 10 Mar 2020 02:51:47 -0400 by karenn1