Antivirus File Scanner API in Java

< Back to Guides

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. To check file for malware using Java, we offer two sample code snippets using different method. Methods includes using OkHttp, and Unirest.

Instructions

  1. Get a free API to use by signing up at FileScanAPI.com and replace REPLACE_ME with your API key.
  2. Update location value in payload with url location of your file.
  3. Update callback value in payload to your webhook url. We recommend using webhook.site as a callback url during test.

For 2 and 3, you can find this documented at Offical Documentation

 

Using OkHttp to check file for virus in Java

OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, """
    {
        "location": "https://filescanapi.com/icon.png",
        "metadata": {
            "filename": "demo.png"
        },
        "callback": "https://webhook.site"
    }
""");
Request request = new Request.Builder()
  .url("https://api.filescanapi.com/v1/file/async")
  .method("POST", body)
  .addHeader("x-api-key", "REPLACE_ME")
  .addHeader("Content-Type", "application/json")
  .build();
Response response = client.newCall(request).execute();

 

Using Unirest to check for malware in

Unirest.setTimeouts(0, 0);
HttpResponse<String> response = Unirest.post("https://api.filescanapi.com/v1/file/async")
  .header("x-api-key", "REPLACE_ME")
  .header("Content-Type", "application/json")
  .body("""
      {
          "location": "https://filescanapi.com/icon.png",
          "metadata": {
              "filename": "demo.png"
          },
          "callback": "https://webhook.site"
      }
  """)
  .asString();