How to Autodetect Content Type and Validate using Java

Cloudmersive
2 min readJun 21, 2022

It’s impossible to trust information coming from the client side of an application. That’s one reason why document validation is so important online; it’s critical to ensure any new information/documents uploaded to your website by a user meets the proper criteria to fulfill a request. Thankfully, there’s an API for that: our Autodetect Content Type & Validate API will (as the title suggests) automatically determine the type of input content received and verify if that the content is valid at depth. Afterward, it’ll report the validation result. In the event there was an error with a document, you’ll receive an error description & error path along with some additional information.

To help you take advantage of this API, we’ve provided ready-to-run code examples you can use to structure your API call in Java, along with instructions to make it as easy & efficient as possible.

Without further ado, let’s begin installing Maven by adding a reference to the repository in pom.xml:

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

Next, add one to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now you can add in the import classes and call the API. Only two parameters are required here; those include your input file, and your Cloudmersive API key (obtainable by visiting our website and registering a free account).

// 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.ValidateDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth 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");
ValidateDocumentApi apiInstance = new ValidateDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
AutodetectDocumentValidationResult result = apiInstance.validateDocumentAutodetectValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentAutodetectValidation");
e.printStackTrace();
}

--

--

Cloudmersive

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