服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现文件变化监控

Java实现文件变化监控

2020-06-05 15:01大饼酥 Java教程

这篇文章主要介绍了Java实现文件变化监控的实现代码,代码附有注释,分步骤介绍的非常详细,非常不错,具有参考借鉴价值,,需要的朋友可以参考下

一. spring配置文件:

application.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import org.apache.commons.io.monitor.FileAlterationObserver;
/**
* 文件监测器角色
*/
public interface FileMonitor {
/**
* 注册观察器
* @param observer 观察器
*/
void addObserver(FileAlterationObserver observer);
/**
* 删除观察器
* @param observer 观察器
*/
void removeObserver(FileAlterationObserver observer);
/**
* 获取注册的所有观察器
* @return 观察器集合
*/
Iterable<FileAlterationObserver> getObservers();
/**
* 启动监测器
*/
void start();
/**
* 停止监测器
*/
void stop();
/**
* 获取监测间隔时间
* @return 间隔时间(单位:毫秒)
*/
long getInterval();
}

  2. 实现类:FileMonitorImpl.class

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.concurrent.ThreadFactory;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
/**
* 监测器,监测时间间隔,设置文件观察器
*/
public class FileMonitorImpl implements FileMonitor{
private final FileAlterationMonitor monitor;
/**
* 监测器线程名称
*/
private static final String MONITOR_THREAD_NAME = "File MONITOR Daemon";
/**
* 监测器线程Daemon标记
*/
private static final boolean DAEMON = false;
/**
* 定义监测时间间隔、文件观察器
* @param interval 监测时间间隔
* @param observer 文件观察者
*/
FileMonitorImpl(int interval, final FileAlterationObserver observer) {
this(interval, observer,
new BasicThreadFactory.Builder().
namingPattern(MONITOR_THREAD_NAME).daemon(DAEMON).build());
}
/**
* 定义监测时间间隔、文件观察器和线程工厂
* @param interval 监测时间间隔
* @param observer 文件观察器
* @param factory 线程工厂
*/
FileMonitorImpl(int interval, final FileAlterationObserver observer,
final ThreadFactory factory) {
this.monitor = new FileAlterationMonitor(interval, new FileAlterationObserver[] { observer });
monitor.setThreadFactory(factory);
}
/**
* 添加文件观察器
* @param observer
*/
@Override
public void addObserver(FileAlterationObserver observer) {
monitor.addObserver(observer);
}
/**
* 删除文件观察器
* @param observer
*/
@Override
public void removeObserver(FileAlterationObserver observer) {
monitor.removeObserver(observer);
}
/**
* 获取注册的所有观察器
* @return
*/
@Override
public Iterable<FileAlterationObserver> getObservers() {
return monitor.getObservers();
}
/**
* 启动监测器
*/
@Override
public void start() {
try {
monitor.start();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 停止监测器
*/
@Override
public void stop() {
try {
monitor.stop();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取监测时间间隔
*/
@Override
public long getInterval() {
return monitor.getInterval();
}
}

五. 文件观察器

  1. 接口:FileObserver.class

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.File;
import org.apache.commons.io.monitor.FileAlterationListener;
/**
* 文件观察器角色
*/
public interface FileObserver {
/**
* 添加监听器
* @param listener
*/
void addListener(final FileAlterationListener listener);
/**
* 删除监听器
* @param listener
*/
void removeListener(final FileAlterationListener listener);
/**
* 获取注册的监听器
* @return
*/
Iterable<FileAlterationListener> getListeners();
/**
* 初始化观察器
* @throws Exception
*/
void initialize() throws Exception;
/**
* 销毁观察器
* @throws Exception
*/
void destroy() throws Exception;
/**
* 获取观察的目录
* @return
*/
File getDirectory();
/**
* 获取文件过滤器
*
* @return
*/
public FileFilter getFilter();
}

  2. 实现类:FileObserverImpl.class

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
/**
* 文件观察器
*
* 当有文件创建、删除、或变更动作时,则消息通知监听器
*/
public class FileObserverImpl extends FileAlterationObserver implements FileObserver{
private static final long serialVersionUID = -7239227289538993830L;
/**
* 文件过滤器
*/
private final FileFilter filter;
/**
* 设置要监听观察的目录,并设置文件过滤器和监听器,用以观察指定具有指定扩展名的文件
* @param dir 观察监听的目录
* @param filter 文件过滤器
* @param listener 文件监听器
*/
public FileObserverImpl(String dir, final FileFilter filter,
FileAlterationListener listener) {
super(dir, filter, (IOCase) null);
addListener(listener);
this.filter = filter;
File directory = new File(dir);
// 如果目录不存在
if(!directory.exists()) {
try {
FileUtils.forceMkdir(directory);
}
catch (IOException e) {
e.printStackTrace();
}
}
// 如果存在的是文件
else if(directory.exists() && directory.isFile()) {
try {
FileUtils.forceDelete(directory);
FileUtils.forceMkdir(directory);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 添加监听器
*/
@Override
public void addListener(final FileAlterationListener listener) {
super.addListener(listener);
}
/**
* 移除监听器
*/
@Override
public void removeListener(final FileAlterationListener listener) {
super.removeListener(listener);
}
/**
* 获取观察者对象的所有监听器
*/
@Override
public Iterable<FileAlterationListener> getListeners() {
return super.getListeners();
}
/**
* 初始化文件观察者
*/
@Override
public void initialize() throws Exception {
super.initialize();
}
/**
* 销毁文件观察者
*/
@Override
public void destroy() throws Exception {
super.destroy();
}
/**
* 获取所观察的目录
*/
@Override
public File getDirectory() {
return super.getDirectory();
}
/**
* 获取文件过滤器
* @return
*/
public FileFilter getFilter() {
return filter;
}
}

六. 文件监听器:

FileListener.class

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.File;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;
/**
* 文件监听器
*/
public final class FileListener extends FileAlterationListenerAdaptor {
/**
* 文件创建时执行的动作
*/
@Override
public void onFileCreate(File file) {
// To do something
}
/**
* 文件删除(转移)时执行的动作
*/
@Override
public void onFileDelete(File file) {
// To do something
}
/**
* 文件内容改变时执行的动作
*/
@Override
public void onFileChange(File file) {
// To do something
}
/**
* 开始执行监听时执行的动作
*/
@Override
public void onStart(FileAlterationObserver observer) {
// To do something
}
/**
* 停止监听时执行的动作
*/
@Override
public void onStop(FileAlterationObserver observer) {
// To do something
}
}

七. 文件过滤器

  1. 接口:FileFilter.class

?
1
2
3
4
5
6
7
8
9
10
11
/**
* 文件过滤器角色,扩展自java.io.FileFilter
*/
public interface FileFilter extends java.io.FileFilter {
/**
* 获取定义的扩展名
*
* @return
*/
String[] getExtensions();
}

  2. 实现类:

FileFilterImpl.class

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.File;
import org.apache.commons.io.FilenameUtils;
/**
* 文件过滤器
*/
public class FileFilterImpl implements FileFilter{
private String[] extensions;
public FileFilterImpl(String... extensions) {
this.extensions = extensions;
}
/**
* 是否接受该文件
*/
@Override
public boolean accept(File pathname) {
return FilenameUtils.isExtension(pathname.getName(), extensions);
}
/**
* 获取定义的扩展名
* @return
*/
@Override
public String[] getExtensions() {
return extensions;
}
}

以上所述是小编给大家介绍的Java实现文件变化监控,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/Mr-kevin/archive/2016/08/18/5784443.html

延伸 · 阅读

精彩推荐