一. ฤดูใบไม้ผลิ配置文件:
Application.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: บริบท = "http:/ xmlns: dwr = "http://www.directwebremoting.org/schema/spring-dwr" xmlns: jaxrs = "http://cxf.apache.org/jaxrs" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsdhttp://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" id = "monitor" class = "com.interfaces.file.monitor.fileMonitorImpl"> <constructor-arg index = "0" value = "10,000" /> <!-监测时间间隔, 单位: id = "observer" class = "com.interfaces.file.monitor.fileobserverimpl"> <constructor-Arg index = "0" value = "d: // uploaddir"/> <! -> </ebean> <!-文件监听器-> <bean id = "Listener" class = "com.interfaces.file.Monitor.FileListener"/> <!-文件过滤器-> <bean id = "ตัวกรอง" class = "com.interfaces.file.monitor.filefilterimpl" -> <constructor-arg index = "0" value = "xml"/> </ebean> </ebeans>
二. ฤดูใบไม้ผลิ上下文加载监听器:
SpringContextLoaderListener.class
นำเข้า javax.servlet.servletContextEvent; นำเข้า org.springframework.web.context.contextloaderlistener; คลาสสาธารณะ SpringContextloaderListener ขยายบริบท listener {@OverridePublic getScanner (); // 启动目录扫描器 scanner.start ();} @overridepublic void contextroyed (เหตุการณ์ servletContextEvent) {scanner fileMonitor = getScanner (); // 关闭目录扫描器 scanner.stop () super.ConTextDestDestRoyed (เหตุการณ์); getCurrentWebapplicationContext (). getBean (fileMonitor.class);}}三. เว็บ工程配置文件:
web.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: web = "http://xmlns.jcp.org/xml 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 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>
四. 文件监测器
1. 接口: filemonitor.class
นำเข้า org.apache.commons.io.monitor.filealterationobserver;/*** 文件监测器角色*/อินเตอร์เฟสสาธารณะ filemonitor {/*** 注册观察器*@param Observer 观察器*/void addobserver /*** 获取注册的所有观察器*@return 观察器集合*/iteRable <TelealterationObserver> getObServers (); /*** 启动监测器*/void start (); /*** 停止监测器*/void stop (); /*** 获取监测间隔时间*@return 间隔时间(单位: 毫秒)*/long getInterval (); - 2. 实现类: filemonitorimpl.class
นำเข้า java.util.concurrent.threadfactory; นำเข้า org.apache.commons.io.monitor.filealterationMonitor; นำเข้า org.apache.commons.io.mon.filealterationObserver; นำเข้า org.apache.Commons.lang3.concurrent.basicthread; ใช้ filemonitor {ส่วนตัว filealterationMonitor monitor;/*** 监测器线程名称*/สตริงสุดท้ายคงที่ส่วนตัว monitor_thread_name = "ไฟล์ตรวจสอบไฟล์ daemon";/*** 监测器线程 daemon 标记*/private private final boolean daemon = false;/*** 定义监测时间间隔、文件观察器*@param interval {this (Interval, Observer, New BasicthreadFactory.builder (). NamingPattern (monitor_thread_name) .daemon (daemon) .build ()); } /*** 定义监测时间间隔、文件观察器和线程工厂* @param Interval 监测时间间隔* @param Observer 文件观察器* @param Factory 线程工厂* /fileMonitorimpl (ช่วงเวลา int, filealterationObserver ผู้สังเกตการณ์สุดท้าย, โรงงาน ThreadFactory สุดท้าย) }/*** 添加文件观察器*@param observer*/ @overridepublic void addobserver (filealterationobserver observer) {monitor.addobserver (ผู้สังเกตการณ์);}/*** 删除文件观察器*@param observer*/ @overridepublic {monitor.removeobserver (Observer);}/*** 获取注册的所有观察器*@return*/@overridepublic iterable <filealterationobserver> getobservers () {return monitor.getobservers ();}/*** 启动监测器*//@@overridepublic start {e.printstacktrace ();}}/*** 停止监测器*/@overridepublic void หยุด () {ลอง {monitor.stop ();} catch (ข้อยกเว้น e) {e.printstacktrace ();}}/*** 获取监测时间间隔*//@overridepublic -五. 文件观察器
1. 接口: fileobserver.class
นำเข้า java.io.file; นำเข้า org.apache.Commons.io.mon.filealterationListener;/*** 文件观察器角色*/ส่วนต่อประสานสาธารณะ fileobserver {/*** 添加监听器*@param listener*/void addlistener ผู้ฟัง);/*** 获取注册的监听器* @return*/iteRable <TelealterationListener> getListeners (); /*** 初始化观察器*@throws Exception*/void Initialize () โยนข้อยกเว้น; /*** 销毁观察器*@throws Exception*/void destroy () โยนข้อยกเว้น; /*** 获取观察的目录* @return*/file getDirectory ();/*** 获取文件过滤器** @return*/public filefilter getFilter ();} 2. 实现类: fileobserverimpl.class
นำเข้า java.io.file; นำเข้า java.io.ioException; นำเข้า org.apache.commons.io.fileutils; นำเข้า org.apache.Commons.io.iocase; นำเข้า org.apache.Commons.io.monitor.filealteraterationListener;当有文件创建、删除、或变更动作时, 则消息通知监听器*/คลาสสาธารณะ FileobServerImpl ขยาย FileAlterationObserver ใช้ fileobserver {ส่วนตัวคงที่สุดท้าย serialversionuid = -723922728953893830l;/*** 文件过滤器*/ตัวกรองไฟล์สุดท้าย Listener 文件监听器*/Public FileobServerImpl (String Dir, ตัวกรอง filefilter สุดท้าย, ผู้ฟัง FilealterationListener) {super (dir, ตัวกรอง, (iocase) null); addListener (ผู้ฟัง); this.filter = ตัวกรอง; ไดเรกทอรีไฟล์ = ไฟล์ใหม่ (dir); // 如果目录不存在ถ้า (! directory.exists ()) {ลอง {fileutils.forcemkdir (ไดเรกทอรี);} catch (ioexception e) {e.printstacktrace ();}} // 如果存在的是文件{fileutils.forcedelete (ไดเรกทอรี); fileutils.forcemkdir (ไดเรกทอรี);} catch (ioexception e) {e.printstacktrace ();}}}/*** 添加监听器*/@superristePublic移除监听器*/@overridepublic void removelistener (ผู้ฟัง filealterationListener ขั้นสุดท้าย) {super.remoVelistener (ผู้ฟัง);}/*** 获取观察者对象的所有监听器*/@overridepublic iterable เริ่มต้น () โยนข้อยกเว้น {super.initialize ();}/*** 销毁文件观察者*/@overridepublic Void ทำลาย () พ่นข้อยกเว้น {super.destroy ();}/*** 获取所观察的目录*/@overridepublic file*/@returned) กรอง;} }六. 文件监听器:
FileListener.class
นำเข้า java.io.file; นำเข้า org.apache.commons.io.monitor.filealterationListeneradaptor; นำเข้า org.apache.Commons.io.onitor.fileAlterationObserver;/*** 文件监听器*/FileListener ระดับสุดท้าย onfileCreate (ไฟล์ไฟล์) {// ทำอะไรบางอย่าง}/*** 文件删除(转移) 时执行的动作*/@overridepublic เป็นโมฆะ onfiledelete (ไฟล์ไฟล์) {// ทำอะไร}/*** 文件内容改变时执行的动作*/@overridepublic onfilechange (ไฟล์ไฟล์) {// OnStart (ผู้สังเกตการณ์ filealterationobserver) {// ทำอะไรบางอย่าง}/*** 停止监听时执行的动作*/@overridepublic เป็นโมฆะ onstop (ผู้สังเกต七. 文件过滤器
1. 接口: filefilter.class
/*** 文件过滤器角色, 扩展自 java.io.filefilter*/public interface filefilter ขยาย java.io.filefilter {/*** 获取定义的扩展名** @return*/string [] getextensions ();};2. 实现类:
FileFilterimpl.class
นำเข้า java.io.file; นำเข้า org.apache.commons.io.filenameutils;/*** 文件过滤器*/คลาสสาธารณะ FileFilterimpl ใช้ filefilter {สตริงส่วนตัว [] ส่วนขยาย; fileFilterimpl สาธารณะ filenameutils.isextension (pathname.getName (), ส่วนขยาย); }/*** 获取定义的扩展名*@return*/ @endridepublic String [] getExtensions () {return extensions;}}以上所述是小编给大家介绍的 Java 实现文件变化监控, 希望对大家有所帮助, 如果大家有任何疑问请给我留言, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!