开发者

How to scan a file with antivirus on upload in Java? [closed]

开发者 https://www.devze.com 2023-02-05 01:24 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 7 years ago.

Improve this question

I am working on an application which requires file upload and it also requires to scan the file with available antivirus on server.

I have heard abt APIS available from Symantec for application server.

Situatuion is like, I need to deploy the application at different places in the future. So, I am thinking to place a configuration file from where I am going to fetch available Antivirus & its path.

I want to use any available antivirus on server and then using command line, I want to pass file name and result back.

I am confused in 开发者_StackOverflow社区passing file and retrieving back results.

Is it possible?


Use the following code.

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);

Commnad line would be better option for you. then read the log file to solve the problem.


I just googled up and found an interesting article have a look at here

To implement a virus file scan in Java, a third-party package needs to be used. For the purposes of this article, I will use Symantec Scan Engine (SSE) package, which comes with Java APIs. This package is an application that serves as a TCP/IP server and has a programming interface and enables Java applications to incorporate support for content scanning technologies. For this article, I used Symantec Scan Engine 5.1, which is available as a Unix or Windows install.

quick reference:

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

then

catch (Exception ex) {
   logger.error(ex);
   if (ex instanceof VirusException) {
      // do something here
   }
   else {
      // there was some other error – handle it
   }
}


Use VirusTotal Public API V2.0 implementation in Java and Go written by VIGHNESWAR RAO
available at https://code.vighnesh.org/virustotal
it has rich features to scan files, URLs, domains, IP addresses and to get a detailed report about scan.
with this API your files will be scanned with 56 antivirus engines. and all antivirus engines are run in virustotal cloud so especially you no need to maintain or run any antivirus engines.
an important feature of this API is it has methods to accept java.io.FileInputStream or java.io.File as arguments.
All API features are clearly explained with examples.
to use this api
step-1: create an account in http://virustotal.com (VirusTotal, a subsidiary of Google) and get API key
step-2: visit https://code.vighnesh.org/virustotal and download required jar files
step-3: just use the methods provided by API.
you can use examples provided at
Java : https://code.vighnesh.org/virustotal/Java/examples.html
Go : https://code.vighnesh.org/virustotal/Go/examples.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号