package com.superl;
/**
* @author superl www.superl.org
* @version V1.0
* @Package com.superl
* @Description 监控功能
* @date 2017/4/11 下午6:07
*/
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.http.ParseException;
import java.io.*;
import java.nio.file.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.Properties;
import static java.nio.file.StandardWatchEventKinds.*;
public class MyService {
private WatchService watchService = null;
private String filePrefix;
private String monitoredPath;
private Boolean checkPrefix;
private Boolean monitoredDir;
private Boolean monitoredMD;
private String ruledoutDir;
private String mobileNum;
private String userName;
private String passWord;
private String smsContent;
private String eventMethod;
public void initialize() throws IOException{
Properties prop = new Properties();
InputStream inputStream = new FileInputStream("config.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
prop.load(bf);
inputStream.close(); // 关闭流
/*
InputStream in = new BufferedInputStream(new FileInputStream("config.properties"));
Properties prop = new Properties();
prop.load(in);
*/
filePrefix = prop.getProperty("file_prefix");
monitoredPath = prop.getProperty("monitored_path");
checkPrefix = new Boolean(prop.getProperty("check_prefix"));
monitoredDir = new Boolean(prop.getProperty("monitored_directory"));
monitoredMD = new Boolean(prop.getProperty("monitored_move_delete"));
ruledoutDir = prop.getProperty("ruledout_dir");
mobileNum = prop.getProperty("mobile");
userName = prop.getProperty("username");
passWord = prop.getProperty("password");
//System.out.println(ruledoutDir); 测试是否中文乱码
watchService = FileSystems.getDefault().newWatchService();
Paths.get(monitoredPath).register(watchService, ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);
File file = new File(monitoredPath);
LinkedList<File> fList = new LinkedList<File>();
fList.addLast(file);
while (fList.size() > 0 ) {
File f = fList.removeFirst();
if(f.listFiles() == null)
continue;
for(File file2 : f.listFiles()){
if (file2.isDirectory()){//下一级目录
fList.addLast(file2);
//依次注册子目录
Paths.get(file2.getAbsolutePath()).register(watchService
, StandardWatchEventKinds.ENTRY_CREATE
, StandardWatchEventKinds.ENTRY_MODIFY
, StandardWatchEventKinds.ENTRY_DELETE);
}
}
}
}
public void doMonitor() throws InterruptedException,IOException{
final Properties PROPERTIES = new Properties(System.getProperties());
String separator = PROPERTIES.getProperty("file.separator");
while(true){
try {
WatchKey key = watchService.take();
for(WatchEvent<?> event : key.pollEvents()){
WatchEvent.Kind kind = event.kind();
if(kind == OVERFLOW){
//事件可能lost or discarded
continue;
}
WatchEvent<Path> e = (WatchEvent<Path>)event;
//获取路径
Path path = (Path) key.watchable();
//获取分隔符号
String fullpath = path.toString()+separator;
//获取文件名
Path fileName = e.context();
//拼接文件名称和路径
String filepath = fullpath+fileName;
//获取文件后缀
String prefix = fileName.toString().substring(fileName.toString().lastIndexOf(".")+1);
File theFile = new File(filepath);
//判断是文件还是目录
if(theFile.isFile()){
//判断是否是白名单目录
String[] ruledoutDirArray = ruledoutDir.split(",");
Boolean haveStr = false;
if(ruledoutDirArray!=null||ruledoutDirArray.length!=0){
haveStr = Arrays.asList(ruledoutDirArray).contains(path.toString());
}else{
haveStr = false;
}
if(!haveStr){
if(checkPrefix){
if(prefix.equals(filePrefix)){
if(kind.toString().equals("ENTRY_CREATE")){
eventMethod = "被新建";
}else if (kind.toString().equals("ENTRY_MODIFY")){
eventMethod = "被修改";
}else{
eventMethod = "被操作";
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
smsContent = "文件:"+filepath+eventMethod+",时间:"+df.format(new Date());
//输出提示信息
System.out.println(smsContent);
//发送提示信息到手机短信
ChuanglanSMS client = new ChuanglanSMS(userName,passWord);
CloseableHttpResponse response = null;
try {
response = client.sendMessage(mobileNum,smsContent);
if(response != null && response.getStatusLine().getStatusCode()==200){
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("短信发送成功,内容为:"+smsContent);
}
}catch (ParseException p1) {
System.out.println("短信发送成失败");
p1.printStackTrace();
}catch (IOException i1) {
System.out.println("短信发送成失败");
i1.printStackTrace();
}
client.close();
}
}else{
if(kind.toString().equals("ENTRY_CREATE")){
eventMethod = "被新建";
}else if (kind.toString().equals("ENTRY_MODIFY")){
eventMethod = "被修改";
}else{
eventMethod = "被操作";
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
smsContent = "文件:"+filepath+eventMethod+",时间:"+df.format(new Date());
//输出提示信息
System.out.println(smsContent);
//发送提示信息到手机短信
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的跨平台服务器文件安全监控软件设计与实现.rar (17个子文件)
基于Java的跨平台服务器文件安全监控软件设计与实现
Web-Security-Monitored.iml 648B
src
META-INF
MANIFEST.MF 51B
com
superl
ChuanglanSMS.java 4KB
MyService.java 15KB
ReadWriteFile.java 4KB
Main.java 958B
config.properties 275B
out
production
Web-Security-Monitored
META-INF
MANIFEST.MF 51B
com
superl
Main.class 796B
ReadWriteFile.class 4KB
MyService.class 9KB
ChuanglanSMS.class 4KB
artifacts
Web_Security_Monitored_jar
Web-Security-Monitored.jar 1.08MB
.gitignore 16B
lib
httpcore-4.4.4.jar 319KB
httpclient-4.5.1.jar 716KB
commons-logging-1.1.1.jar 59KB
共 17 条
- 1
资源评论
爱吃苹果的Jemmy
- 粉丝: 75
- 资源: 1148
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功