Java實現系統目錄實時監聽更新。
阿新 • • 發佈:2017-05-25
warning xtend absolute ava single sdn data- sdk spl
Servlet的實現類:
轉:http://blog.csdn.net/feiying2j/article/details/50719648 附上源碼下載地址: http://download.csdn.net/detail/feiying2j/9439698
SDK1.7新增的nio WatchService能完美解決這個問題。美中不足是如果部署在window系統下會出現莫名其妙的文件夾占用異常導致子目錄監聽失效,linux下則完美運行。這個問題著實讓人頭疼。如果有童鞋找到問題根源請一起探討。
這裏簡單的列出用Servlet實現的基本類供大家參考。首先是核心的實現類。
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
package com.event;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
import java.io.File; |
來自CODE的代碼片
DirectoryWatcher.javaServlet的實現類:
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
package com.demo; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Observable; import java.util.Observer; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.entry.VedioFile; import com.event.DirectoryWatcher; import com.event.FileWatchEventArgs; public class GetList extends HttpServlet implements Observer { private static final long serialVersionUID = 3715567236188574915L; private String PATH; private String JsonTreeStr = null; private static List<VedioFile> vedioFileList; private static ThreadLocal<String> SubRequestURL = new ThreadLocal<String>(); public static ThreadLocal<String> getSubRequestURL() { return SubRequestURL; } public static List<VedioFile> getVedioFileList() { return vedioFileList; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PATH = request.getServletContext().getRealPath("/") + "/curse"; request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (vedioFileList == null) { PATH = request.getServletContext().getRealPath("/") + "curse"; System.out.println(PATH); setVedioFileList(); // 建立目錄監聽,註冊觀察者為本身 DirectoryWatcher dw = new DirectoryWatcher(PATH); dw.addObserver(this); dw.execute(); } SubRequestURL .set(request.getRequestURL().toString().split("getList")[0]); JsonTreeStr = vedioFileList.toString(); response.setContentType("text/xml;charset=utf-8"); response.setHeader("Cache-Control", "no-cache"); response.getWriter().write(JsonTreeStr); } @Override // 實現Observer觀察者接口,接收通知 public void update(Observable o, Object arg) { FileWatchEventArgs args = (FileWatchEventArgs) arg; String name = args.getName(); String kind = args.getKind(); String dir = args.getDir(); File file = new File(dir + File.separator + name); if (kind.equals("ENTRY_CREATE")) { addVedioFile(file); } else if (kind.equals("ENTRY_DELETE")) { removeVedioFile(file); } } private void removeVedioFile(File file) { VedioFile nowVf = getNowVedioFile(file); if (nowVf != null) { for (int i = vedioFileList.size() - 1; i >= 0; i--) { if (nowVf.getPath() .equals(vedioFileList.get(i).getParentPath())) { vedioFileList.remove(i); } } vedioFileList.remove(nowVf); } } private VedioFile getNowVedioFile(File file) { VedioFile nowVf = null; for (VedioFile vf : vedioFileList) { if (file.getAbsolutePath().equals(vf.getPath())) { nowVf = vf; break; } } return nowVf; } private void setVedioFileList() { File file = new File(PATH); vedioFileList = new ArrayList<VedioFile>(); getVedioFiles(file, vedioFileList); } private void getVedioFiles(File file, List<VedioFile> vfs) { if (file.isDirectory()) { File[] files = file.listFiles(); for (File f : files) { getVedioFiles(f, vfs); } } addVedioFile(file); } private void addVedioFile(File file) { VedioFile vf = new VedioFile(); vf.setParentPath(file.getParent()); vf.setPath(file.getAbsolutePath()); vf.setName(file.getName()); if (file.getName().toLowerCase().endsWith(".mp4")) { vf.setUrl("vedio?url=" + MD5Utils.md5(vf.getPath())); } vedioFileList.add(vf); } } |
來自CODE的代碼片
GetList.java轉:http://blog.csdn.net/feiying2j/article/details/50719648 附上源碼下載地址: http://download.csdn.net/detail/feiying2j/9439698
Java實現系統目錄實時監聽更新。