一. 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"> <!-- 文件監(jiān)測器 --> <bean id= "monitor" class = "com.interfaces.file.monitor.FileMonitorImpl" > <constructor-arg index= "0" value= "10000" /> <!-- 監(jiān)測時(shí)間間隔,單位:毫秒 --> <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" /> <!-- 文件監(jiān)聽器 --> </bean> <!-- 文件監(jiān)聽器 --> <bean id= "listener" class = "com.interfaces.file.monitor.FileListener" /> <!-- 文件過濾器 --> <bean id= "filter" class = "com.interfaces.file.monitor.FileFilterImpl" > <!-- 指定文件擴(kuò)展名,只有指定的擴(kuò)展名文件會(huì)被處理。 不同的擴(kuò)展名間以 "," 間隔,如:xml,txt,bak --> <constructor-arg index= "0" value= "xml" /> </bean> </beans> |
二. spring上下文加載監(jiān)聽器:
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(); // 啟動(dòng)目錄掃描器 scanner.start(); } @Override public void contextDestroyed(ServletContextEvent event) { FileMonitor scanner = getScanner(); // 關(guān)閉目錄掃描器 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> |
四. 文件監(jiān)測器
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; /** * 文件監(jiān)測器角色 */ public interface FileMonitor { /** * 注冊(cè)觀察器 * @param observer 觀察器 */ void addObserver(FileAlterationObserver observer); /** * 刪除觀察器 * @param observer 觀察器 */ void removeObserver(FileAlterationObserver observer); /** * 獲取注冊(cè)的所有觀察器 * @return 觀察器集合 */ Iterable<FileAlterationObserver> getObservers(); /** * 啟動(dòng)監(jiān)測器 */ void start(); /** * 停止監(jiān)測器 */ void stop(); /** * 獲取監(jiān)測間隔時(shí)間 * @return 間隔時(shí)間(單位:毫秒) */ long getInterval(); } |
2. 實(shí)現(xiàn)類: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; /** * 監(jiān)測器,監(jiān)測時(shí)間間隔,設(shè)置文件觀察器 */ public class FileMonitorImpl implements FileMonitor{ private final FileAlterationMonitor monitor; /** * 監(jiān)測器線程名稱 */ private static final String MONITOR_THREAD_NAME = "File MONITOR Daemon" ; /** * 監(jiān)測器線程Daemon標(biāo)記 */ private static final boolean DAEMON = false ; /** * 定義監(jiān)測時(shí)間間隔、文件觀察器 * @param interval 監(jiān)測時(shí)間間隔 * @param observer 文件觀察者 */ FileMonitorImpl( int interval, final FileAlterationObserver observer) { this (interval, observer, new BasicThreadFactory.Builder(). namingPattern(MONITOR_THREAD_NAME).daemon(DAEMON).build()); } /** * 定義監(jiān)測時(shí)間間隔、文件觀察器和線程工廠 * @param interval 監(jiān)測時(shí)間間隔 * @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); } /** * 獲取注冊(cè)的所有觀察器 * @return */ @Override public Iterable<FileAlterationObserver> getObservers() { return monitor.getObservers(); } /** * 啟動(dòng)監(jiān)測器 */ @Override public void start() { try { monitor.start(); } catch (Exception e) { e.printStackTrace(); } } /** * 停止監(jiān)測器 */ @Override public void stop() { try { monitor.stop(); } catch (Exception e) { e.printStackTrace(); } } /** * 獲取監(jiān)測時(shí)間間隔 */ @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 { /** * 添加監(jiān)聽器 * @param listener */ void addListener( final FileAlterationListener listener); /** * 刪除監(jiān)聽器 * @param listener */ void removeListener( final FileAlterationListener listener); /** * 獲取注冊(cè)的監(jiān)聽器 * @return */ Iterable<FileAlterationListener> getListeners(); /** * 初始化觀察器 * @throws Exception */ void initialize() throws Exception; /** * 銷毀觀察器 * @throws Exception */ void destroy() throws Exception; /** * 獲取觀察的目錄 * @return */ File getDirectory(); /** * 獲取文件過濾器 * * @return */ public FileFilter getFilter(); } |
2. 實(shí)現(xiàn)類: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; /** * 文件觀察器 * * 當(dāng)有文件創(chuàng)建、刪除、或變更動(dòng)作時(shí),則消息通知監(jiān)聽器 */ public class FileObserverImpl extends FileAlterationObserver implements FileObserver{ private static final long serialVersionUID = -7239227289538993830L; /** * 文件過濾器 */ private final FileFilter filter; /** * 設(shè)置要監(jiān)聽觀察的目錄,并設(shè)置文件過濾器和監(jiān)聽器,用以觀察指定具有指定擴(kuò)展名的文件 * @param dir 觀察監(jiān)聽的目錄 * @param filter 文件過濾器 * @param listener 文件監(jiān)聽器 */ 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(); } } } /** * 添加監(jiān)聽器 */ @Override public void addListener( final FileAlterationListener listener) { super .addListener(listener); } /** * 移除監(jiān)聽器 */ @Override public void removeListener( final FileAlterationListener listener) { super .removeListener(listener); } /** * 獲取觀察者對(duì)象的所有監(jiān)聽器 */ @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; } } |
六. 文件監(jiān)聽器:
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; /** * 文件監(jiān)聽器 */ public final class FileListener extends FileAlterationListenerAdaptor { /** * 文件創(chuàng)建時(shí)執(zhí)行的動(dòng)作 */ @Override public void onFileCreate(File file) { // To do something } /** * 文件刪除(轉(zhuǎn)移)時(shí)執(zhí)行的動(dòng)作 */ @Override public void onFileDelete(File file) { // To do something } /** * 文件內(nèi)容改變時(shí)執(zhí)行的動(dòng)作 */ @Override public void onFileChange(File file) { // To do something } /** * 開始執(zhí)行監(jiān)聽時(shí)執(zhí)行的動(dòng)作 */ @Override public void onStart(FileAlterationObserver observer) { // To do something } /** * 停止監(jiān)聽時(shí)執(zhí)行的動(dòng)作 */ @Override public void onStop(FileAlterationObserver observer) { // To do something } } |
七. 文件過濾器
1. 接口:FileFilter.class
1
2
3
4
5
6
7
8
9
10
11
|
/** * 文件過濾器角色,擴(kuò)展自java.io.FileFilter */ public interface FileFilter extends java.io.FileFilter { /** * 獲取定義的擴(kuò)展名 * * @return */ String[] getExtensions(); } |
2. 實(shí)現(xiàn)類:
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); } /** * 獲取定義的擴(kuò)展名 * @return */ @Override public String[] getExtensions() { return extensions; } } |
以上所述是小編給大家介紹的Java實(shí)現(xiàn)文件變化監(jiān)控,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/Mr-kevin/archive/2016/08/18/5784443.html