RSS

Scan document for virus – JAVA

07 Mar

Hi folks,

Here is the working code for to scan uploaded document and verify any virus effected or not.

Just follow the below steps to get execute and see the result.

1. create Interface VirusScanDemo

import org.apache.struts.upload.FormFile;

public interface VirusScanDemo {
public boolean scanForVirus(FormFile document);

}

2.create a class with method that implements above interface method.

public boolean scanForVirus(FormFile document){
boolean isSafe = true;
InputStream isStream = null;
try {
ftp.connect(serverName);
// After connection attempt, check the reply code to verify the success.
int reply = ftp.getReplyCode();

if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
log.info(“FTP server refused connection.”);
return false;
}
//login
ftp.login( userName, passwd );
// transfer file
if( destinationFolderName != null && destinationFolderName.trim().length() > 0 ){
// change the working directory
ftp.changeWorkingDirectory( destinationFolderName.trim() );
}
ftp.setRemoteVerificationEnabled(false);
isStream = document.getInputStream();
boolean ftpStatus = ftp.storeFile(document.getFileName(), isStream);
String replyString = ftp.getReplyString();
if((!ftpStatus)||((replyString.indexOf(“226”)) < 0)){
log.info(“Virus scan result -> ” + replyString + “File Name : ” + document.getFileName());
isSafe = false;
}
ftp.logout();
} catch(IOException e) {
isSafe = false;
log.info(“IOException in FortigateFTPUtil scanForVirus() “+e);
} finally {
if(ftp.isConnected()) {
try {
isStream.close();
ftp.disconnect();
} catch(IOException ioe) {
// do nothing
}
}
}
return isSafe;
}

For more details : https://commons.apache.org/net/api-3.1/org/apache/commons/net/ftp/FTPClient.html

Here is the detailed explanation with example code http://www.developer.com/design/article.php/3711331/Implementing-an-Anti-Virus-File-Scan-in-JEE-Applications.htm

Happy Coding!.

Stay tuned to know more updates.

Have a nice day 🙂

Advertisements
 
Leave a comment

Posted by on March 7, 2013 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: