https://my.oschina.net/u/3616084/blog/1801987
1.启动代码
1 |
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 |
public static void main(String[] args) { FileListener FileListener = new FileListener(); boolean b = false; if(!b){ try { /** 启动文件监听 */ System.out.println("文件监听开始............"); String path = "F:\\mulu"; System.out.println("监听文件路径" + path); FileMonitor m = new FileMonitor(5000); m.monitor(path,FileListener); m.start(); System.out.println("文件监听启动............"); } catch (Exception e) { System.out.println("启动文件监听失败.........." + e.getMessage()); e.printStackTrace(); } } b = true; } |
1 |
1 |
2、文件动作展示代码
1 |
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 |
package org.mh.web.view.admin; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.io.monitor.FileAlterationListener; import org.apache.commons.io.monitor.FileAlterationObserver; import org.mh.web.task.TaskReceiverRunnable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; //根据相关触发的行为,实现相关的业务 public class FileListener implements FileAlterationListener { /**日志工厂*/ protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public void onStart(FileAlterationObserver observer) { logger.info("onStart............."); } @Override public void onDirectoryChange(File directory) { logger.info("[目录修改]:" + directory.getName()); } @Override public void onDirectoryCreate(File directory) { logger.info("[目录新建]:" + directory.getName()); } @Override public void onDirectoryDelete(File directory) { logger.info("[目录删除]:" + directory.getName()); } @Override public void onFileChange(File file) { logger.info("[文件创建修改]: :" + file.getName()); } @Override public void onFileCreate(File file) { logger.info("[文件新建执行]:" + file.getName()); } @Override public void onFileDelete(File file) { logger.info("[文件删除]:" + file.getName()); } @Override public void onStop(FileAlterationObserver observer) { logger.info("onStop............"); } } |
1 |
1 |
3、监控代码
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 |
package org.mh.web.view.admin; import java.io.File; import org.apache.commons.io.monitor.FileAlterationListener; import org.apache.commons.io.monitor.FileAlterationMonitor; import org.apache.commons.io.monitor.FileAlterationObserver; //观察,监控 文件变化 public class FileMonitor { FileAlterationMonitor monitor = null; public FileMonitor(long interval) throws Exception { monitor = new FileAlterationMonitor(interval); } public void monitor(String path, FileAlterationListener listener) { FileAlterationObserver observer = new FileAlterationObserver(new File(path)); monitor.addObserver(observer); observer.addListener(listener); } public void stop() throws Exception{ monitor.stop(); } public void start() throws Exception { monitor.start(); } } |