How to implement virus scan when downloading a file in a Spring 3 MVC application

I need to scan downloaded files for viruses and show the corresponding messages to the user in the Spring 3 MVC application.

Currently, I have configured the file upload using Spring MVC 3. After the file is received in the controller, I must check for the presence of a virus and if the file is infected with any virus, the application should reject this file and show the user a specific message.

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

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

Many thanks.

+4
source share
1 answer

First, you should check which API your installed antivirus software provides.

If there is any Java API (e.g. AVG API), you should use it as shown 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 antivirus software is not provided by the Java API, you can invoke it using the command line, as shown 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 reference: Implementing Antivirus File Scanning in JEE Applications

Hope this helps you.

+7
source

Source: https://habr.com/ru/post/1533468/


All Articles