How to Scan a File for Viruses in Java (Basic Version)

Cloudmersive
2 min readMar 28, 2022

It’s time for your application security policies to dig deeper. Implement the basic iteration of our Virus Scan API (appears as /virus/scan/file on the Cloudmersive API Console) to check files for millions of virus & malware signatures. A variety of file formats are conveniently supported, including Office, PDF, HTML, Flash, and more. See how to connect using Java below.

To start off, you’ll need to install the Java SDK. For Maven, this entails adding a reference to the repository in pom.xml as such:

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

Then, add a reference to the dependency in pom.xml:

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

With installation complete, you may now add imports and call the validation function:

// 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.ScanApi;
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");
ScanApi apiInstance = new ScanApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
VirusScanResult result = apiInstance.scanFile(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ScanApi#scanFile");
e.printStackTrace();
}

And just like that, you’re all set to scan. The below screenshot contains an example response model for reference.

Virus Scan API (Basic) Response Model

--

--

Cloudmersive

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