gpt4 book ai didi

java - 检查上传的文件是否有病毒

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:28:09 25 4
gpt4 key购买 nike

我有一些申请。用户可以上传文件,我将其保存在磁盘上,并在用户需要时返回。我需要对上传的文件实现一些病毒保护。我为这个问题找到了 3 个解决方案:

  1. 使用在线防病毒软件
  2. 在我的服务器上安装防病毒软件并从命令行检查上传的文件
  3. 通过 sdk 或 api 集成防病毒。

我不喜欢第一个解决方案,因为我将我的文件和私有(private)信息发送给其他服务器。我认为第二种解决方案是最好的,但我不知道如何正确实现它。最后一个解决方案很好,但我找不到任何有 java api 的好的和著名的防病毒软件。

请给我一些解决这个问题的方向。 mb 一些建议或文献。解决它的最佳方法是什么?

最佳答案

首先,您必须检查您安装的杀毒软件提供了什么样的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);

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

希望对你有帮助。

关于java - 检查上传的文件是否有病毒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38562418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com