如何在Spring 3 MVC应用程序中实现文件上载的病毒扫描

coo*_*ler 5 java file-upload spring-mvc virus-scanning

我必须扫描上传的文件中的病毒,并在Spring 3 MVC应用程序中向用户显示相应的消息.

目前我已经使用Spring MVC 3配置了文件上传.一旦在控制器中收到文件,我必须检查病毒是否存在,如果文件感染了任何病毒,那么应用程序应拒绝该文件并向用户显示特定消息.

关于如何在Java Spring MVC应用程序中实现它的任何想法.我使用的弹簧版本是3.2.4.

任何这方面的帮助/指针都将受到高度赞赏.

非常感谢.

Shi*_*mar 9

首先,您必须检查已安装的防病毒软件提供的API类型.

如果提供了任何Java API(如AVG API),则必须按如下方式使用它:

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.");
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

如果安装的防病毒软件未提供Java API,则仍可以使用命令行调用它,如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

有一篇有趣的文章供您参考:在JEE应用程序中实现反病毒文件扫描

希望这对你有所帮助.