Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 924
cooler Avatar asked Mar 26 '14 05:03

cooler


People also ask

How would you handle the situation where a user uploads a very large file through a form in your Spring MVC application?

A new attribute "maxSwallowSize" is the key to deal this situation. It should happen when you upload a file which is larger than 2M. Because the 2M is the default value of this new attribute .

Which dependency is needed to support uploading a file in MVC?

The dependency needed for MVC is the spring-webmvc package. The javax. validation and the hibernate-validator packages will be also used here for validation. The commons-io and commons-fileupload packages are used for uploading the file.


1 Answers

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.

like image 94
Shishir Kumar Avatar answered Nov 15 '22 00:11

Shishir Kumar