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

5
我需要在Spring 3 MVC应用程序中扫描上传的文件以检测病毒,并向用户显示适当的消息。

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

您有关于如何在Java Spring MVC应用程序中实现这一点的想法吗?我正在使用的Spring版本是3.2.4。

非常感谢您的帮助/指导。

谢谢!


嘿@Raptor,目前文件上传模块已经可以使用了。我不知道如何在控制器层面上扫描这些文件,所以我需要一些指针来开始处理它。由于我的想法用尽了,我还没有开始处理它。 - cooler
1个回答

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

如果已安装的防病毒软件没有提供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);

这里有一篇有趣的文章供您参考:在JEE应用程序中实现反病毒文件扫描。希望对您有所帮助。

你能提供AVG API的链接吗?感谢你的指引。 - cooler
1
同意,我也找不到这个 API 文档在哪里? - AngryDuck
如果可以采用第二种方法,如何通过杀毒软件界面跟踪病毒扫描的状态? - rolling stone
1
@Shishir Kumar这个API能够返回被扫描的文件状态吗? - rolling stone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接