How to implement virus scan on file upload in a Spring 3 MVC application [closed]
Asked Answered
L

1

5

I have to scan the uploaded files for virus and show appropriate messages to the user in a Spring 3 MVC app.

Currently I have configured file upload using Spring MVC 3. Once the file is recieved in the controller, I have to check for virus presence and if the file is infected with any virus, then application should reject that file and show user a specific message.

Any ideas on how this can be implemented in a Java Spring MVC application. The spring version that I am using is 3.2.4.

Any help/pointers in this direction would be highly appreciated.

thanks a lot.

Lumber answered 26/3, 2014 at 5:46 Comment(1)
hey @Raptor as of now, the file upload module is working. I have no idea how to scan those files in the controller level and that is why i need pointers so I can start on it. I haven't started on it yet as I am running out of ideas.Lumber
S
9

First, you have to check what kind of API does your installed antivirus software provides.

If there is any Java API provided (like AVG API) then you have to use it as below:

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.");
      }
   }
}

If no Java API is provided by the installed antivirus software, then you can still invoke it using command line, as below:

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);

There is an interesting article for your reference: Implementing an Anti-Virus File Scan in JEE Applications

Hope this helps you.

Strathspey answered 26/3, 2014 at 5:53 Comment(4)
can you provide a link to the AVG API? thanks for your pointers.Lumber
agreed i cant find this api documentation anywhere???Beanie
if the method 2 can be followed how to track the status of the virus scan ?.through the antivirus UI ?Cattima
@Shishir Kumar is that api giving repsonse about the status of the file scanned ?Cattima

© 2022 - 2024 — McMap. All rights reserved.