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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22652489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:07:01  来源:igfitidea点击:

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

javaspring-mvcfile-uploadvirus-scanning

提问by cooler

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

我必须扫描上传的文件是否有病毒,并在 Spring 3 MVC 应用程序中向用户显示适当的消息。

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.

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

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.

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

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

在这个方向上的任何帮助/指针将不胜感激。

thanks a lot.

多谢。

采纳答案by Shishir Kumar

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

首先,您必须检查您安装的防病毒软件提供什么样的 API。

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

如果提供了任何 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.");
      }
   }
}

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

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

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

有一篇有趣的文章供您参考:Implementing an Anti-Virus File Scan in JEE Applications

Hope this helps you.

希望这对你有帮助。