How to get document type information in Java

Extension-less files can truly be the bane of productivity. Identifying these unknown file formats by conventional means is tricky, and becomes an even larger problem if you want to integrate an automatic identification system into your app. Today’s topic is how to skip all of this difficulty and get straight to results.

To begin with, we need to unpack our API library with Jitpack. Enter the following two references into your Maven POM file.

Repositories

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Dependencies

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.34</version>
</dependency>
</dependencies>

With our library set up, we are ready to invoke convertDocumentAutodetectGetInfo with the following snippet:

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.ConvertDocumentApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");ConvertDocumentApi apiInstance = new ConvertDocumentApi();File inputFile = new File("/path/to/file"); // File | Input file to perform the operation on.try {AutodetectGetInfoResult result = apiInstance.convertDocumentAutodetectGetInfo(inputFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling ConvertDocumentApi#convertDocumentAutodetectGetInfo");e.printStackTrace();}

That’s it! Our file type will be identified and given back to us in the following format:

{
"Successful": true,
"DetectedFileExtension": "string",
"DetectedMimeType": "string",
"PageCount": 0,
"Author": "string",
"DateModified": "2020-02-05T02:37:28.102Z",
"AlternateFileTypeCandidates": [
{
"Probability": 0,
"DetectedFileExtension": "string",
"DetectedMimeType": "string"
}
]
}
Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store