How to scan a file with antivirus on upload in Java?
Use the following code.
String[] commands = new String[5];
commands[0] = "cmd";
commands[1] = "/c";
commands[2] = "C:\\Program Files\\AVG\\AVG10\\avgscanx.exe";
commands[3] = "/scan=" + filename;
commands[4] = "/report=" + virusoutput;
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(commands);
Commnad line would be better option for you. then read the log file to solve the problem.
I just googled up and found an interesting article have a look at here
To implement a virus file scan in Java, a third-party package needs to be used. For the purposes of this article, I will use Symantec Scan Engine (SSE) package, which comes with Java APIs. This package is an application that serves as a TCP/IP server and has a programming interface and enables Java applications to incorporate support for content scanning technologies. For this article, I used Symantec Scan Engine 5.1, which is available as a Unix or Windows install.
quick reference:
public void scanFile(byte[] fileBytes, String fileName)
throws IOException, Exception {
if (scan) {
AVClient avc = new AVClient(avServer, avPort, avMode);
if (avc.scanfile(fileName, fileBytes) == -1) {
throw new VirusException("WARNING: A virus was detected in
your attachment: " + fileName + "<br>Please scan
your system with the latest antivirus software with
updated virus definitions and try again.");
}
}
}
then
catch (Exception ex) {
logger.error(ex);
if (ex instanceof VirusException) {
// do something here
}
else {
// there was some other error – handle it
}
}
Use VirusTotal Public API V2.0 implementation in Java and Go written by
VIGHNESWAR RAO
available at https://code.vighnesh.org/virustotal
it has rich features to scan files, URLs, domains, IP addresses and to get a detailed report about scan.
with this API your files will be scanned with 56 antivirus engines. and all antivirus engines are run in virustotal cloud so especially you no need to maintain or run any antivirus engines.
an important feature of this API is it has methods to accept java.io.FileInputStream or java.io.File as arguments.
All API features are clearly explained with examples.
to use this api
step-1: create an account in http://virustotal.com (VirusTotal, a subsidiary of Google) and get API key
step-2: visit https://code.vighnesh.org/virustotal and download required jar files
step-3: just use the methods provided by API.
you can use examples provided at
Java : https://code.vighnesh.org/virustotal/Java/examples.html
Go : https://code.vighnesh.org/virustotal/Go/examples.html