java实现文件变化监控的方法实现文件变化监控的方法(推荐推荐)
下面小编就为大家带来一篇java实现文件变化监控的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
一一. spring配置文件:配置文件:application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- 文件监测器 -->
<bean id="monitor"
class="com.interfaces.file.monitor.FileMonitorImpl">
<constructor-arg index="0" value="10000" /> <!-- 监测时间间隔,单位:毫秒 -->
<constructor-arg index="1" ref="observer" /> <!-- 文件观察器 -->
</bean>
<!-- 文件观察器 -->
<bean id="observer"
class="com.interfaces.file.monitor.FileObserverImpl">
<constructor-arg index="0" value="D:\UploadDir"/> <!-- 观察的目录 -->
<constructor-arg index="1" ref="filter"/> <!-- 文件过滤器-->
<constructor-arg index="2" ref="listener"/> <!-- 文件监听器 -->
</bean>
<!-- 文件监听器 -->
<bean id="listener"
class="com.interfaces.file.monitor.FileListener"/>
<!-- 文件过滤器 -->
<bean id="filter"
class="com.interfaces.file.monitor.FileFilterImpl">
<!--
指定文件扩展名,只有指定的扩展名文件会被处理。
不同的扩展名间以 "," 间隔,如:xml,txt,bak
-->
<constructor-arg index="0" value="xml"/>
</bean>
</beans>
二二. spring上下文加载监听器:上下文加载监听器:SpringContextLoaderListener.class
import javax.servlet.ServletContextEvent;
import org.springframework.web.context.ContextLoaderListener;
public class SpringContextLoaderListener extends ContextLoaderListener{
@Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
FileMonitor scanner = getScanner();
// 启动目录扫描器
scanner.start();
}
@Override
public void contextDestroyed(ServletContextEvent event) {
FileMonitor scanner = getScanner();
// 关闭目录扫描器
scanner.stop();
super.contextDestroyed(event);
}
/**
* 获取目录扫描器
* @return
*/
private FileMonitor getScanner() {
return getCurrentWebApplicationContext().getBean(FileMonitor.class);
}
}
三三. web工程配置文件:工程配置文件:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:application.xml
</param-value>
</context-param>
<listener>
<listener-class>com.web.SpringContextLoaderListener</listener-class>
</listener>
</web-app>
四四. 文件监测器文件监测器
1. 接口:FileMonitor.class
import org.apache.commons.io.monitor.FileAlterationObserver;
/**
* 文件监测器角色
*/
public interface FileMonitor {
/**
* 注册观察器
* @param observer 观察器
*/
void addObserver(FileAlterationObserver observer);
/**
* 删除观察器
* @param observer 观察器
*/
void removeObserver(FileAlterationObserver observer);
/**