远控开发记录05_文件目录

远控系统中实现安装了远控app后,注册并登录后会创建一个后台Service读取手机中的文件及其路径信息,同时存入数据库中,Web端再将数据库中的内容以树状的方式显示在主页的左侧,方便用户查看选择。如图

这里写图片描述

这里这是获取了手机中的常用文件的目录结构。供用户进行备份删除等操作。

那笔者接下来讲如何实现将文件目录信息同步更新到数据库。
笔者这里先把文件的名称,路径分别写到list里去
因为文件目录又可能很大。所以我先记录文档和视频的文件路径

public void setfile(String path) {
        File file = new File(path);
        if (file.canRead() && file.exists()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    if (f.canRead()) {
                        if (f.isDirectory()) {
                            setfile(f.getPath());
                        } else {
                            if (f.getName().endsWith(".doc") ||
                                    f.getName().endsWith(".docx") ||
                                    f.getName().endsWith(".pdf") ||
                                    f.getName().endsWith(".ppt") ||
                                    f.getName().endsWith(".pptx") ||
                                    f.getName().endsWith(".xls") ||
                                    f.getName().endsWith(".xlsx")) {
                                docname.add(f.getName());
                                docpath.add(f.getPath());
                            }
                            if (f.getName().endsWith(".avi") ||
                                    f.getName().endsWith(".rmvb") ||
                                    f.getName().endsWith(".rm") ||
                                    f.getName().endsWith(".mp4") ||
                                    f.getName().endsWith(".wav") ||
                                    f.getName().endsWith(".flv") ||
                                    f.getName().endsWith(".mkv")) {
                                vioname.add(f.getName());
                                viopath.add(f.getPath());
                            }
                        }
                    }
                }
            }
        }
    }

再记录图片的文件路径

public void setpic(String path) {
        File file = new File(path);
        if (file.canRead() && file.exists()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    if (f.canRead()) {
                        if (f.isDirectory()) {
                            setpic(f.getPath());
                        } else {
                            if (f.getName().endsWith(".jpg") ||
                                    f.getName().endsWith(".jpeg")) {
                                picname.add(f.getName());
                                picpath.add(f.getPath());
                            }
                        }
                    }
                }
            }
        }
    }

记录之后,一起发送至服务器

public void pread(int type) {
        AsyncHttpClient httpClient = App.httpClient;
        RequestParams params = new RequestParams();
        String url = App.host + "/home/user/read";
        params.put("ClientID", App.cid);
        if (type == 1) {
            params.put("picname", picname);
            params.put("picpath", picpath);
            params.put("docname", docname);
            params.put("docpath", docpath);
            params.put("vioname", vioname);
            params.put("viopath", viopath);
            //   Log.v("hello", "1");
        } else if (type == 2) {
            params.put("docname", docname);
            params.put("docpath", docpath);
            //  Log.v("hello", "2");
        } else if (type == 3) {
            params.put("vioname", vioname);
            params.put("viopath", viopath);
            // Log.v("hello", "3");
        }
        if (type != 4) {
            httpClient.post(url, params, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                    Log.v("hello", "faile" + s);
                }

                @Override
                public void onSuccess(int i, Header[] headers, String s) {
                    if (i == 200) {
                        //    Log.v("hello", s);
                    } else
                    //  Log.v("hello", "fei200");
                    {
                    }
                }
            });
        }
    }

“/home/user/read”的函数如下:

 public function read()
    {
        $often = D('often');
        $user = D('user');
        $tree = D('tree');
        $picname = $_POST['picname'];
        $picpath = $_POST['picpath'];
        $docname = $_POST['docname'];
        $docpath = $_POST['docpath'];
        $vioname = $_POST['vioname'];
        $viopath = $_POST['viopath'];

        $treename = $_POST['treename'];
        $treestatic = $_POST['treestatic'];

        $parent['path'] = $_POST['parent'];
        $cid['ClientID'] = $_POST['ClientID'];
        $uid = $user->where($cid)->find();
        $userid['userid'] = $uid['No'];
        $often->where($userid)->delete();
        if (!empty($treename)) {
            $pid = 0;
            for ($i = 0; $i < count($treename); $i++) {
                $data['userid'] = $userid['userid'];
                $data['text'] = $treename[$i];
                $data['path'] = $parent['path'] . '/' . $treename[$i];
                $data['state'] = $treestatic[$i];
                if (!$tree->where($data)->find()) {
                    $data['tid'] = $pid;
                    $tid = $tree->field('id')->where($parent['path'])->find();
                    if ($tid) {
                        $data['tid'] = $tid['id'];
                    };
                    $tree->add($data);
                }
            }
            echo $treename[0] . ',' . $treename[count($treename) - 1] . $tid['tid'] . ','
                . count($treename);
        } else {
            echo "empty";
        }
        if (!empty($picname)) {
            for ($i = 0; $i < count($picname); $i++) {
                $data['userid'] = $userid['userid'];
                $data['text'] = $picname[$i];
                $data['path'] = $picpath[$i];
                $data['state'] = 'open';
                $data['tid'] = 1;
                if (!$often->where($data)->find())
                    $often->add($data);
            }
            echo $picname[0];
        }
        if (!empty($docname)) {
            for ($i = 0; $i < count($docname); $i++) {
                $data['userid'] = $userid['userid'];
                $data['text'] = $docname[$i];
                $data['path'] = $docpath[$i];
                $data['state'] = 'open';
                $data['tid'] = 2;
                if (!$often->where($data)->find())
                    $often->add($data);
            }
            echo $docname[0];
        }
        if (!empty($vioname)) {
            for ($i = 0; $i < count($vioname); $i++) {
                $data['userid'] = $userid['userid'];
                $data['text'] = $vioname[$i];
                $data['path'] = $viopath[$i];
                $data['state'] = 'open';
                $data['tid'] = 3;
                if (!$often->where($data)->find())
                    $often->add($data);
            }
            echo $vioname[0];
        }
    }

那么到这里为止只是将文件目录上传了一次。如果用户有改变时。我们需要同步更新文件目录。笔者这里使用Fileobserver来监听文件目录的变化。

static class RecursiveFileObserver extends FileObserver {
        /**
         * Only modification events
         */
        public static int CHANGES_ONLY = CREATE | DELETE | CLOSE_WRITE | MOVE_SELF | MOVED_FROM | MOVED_TO;

        List<SingleFileObserver> mObservers;
        String mPath;
        int mMask;

        public RecursiveFileObserver(String path) {
            this(path, ALL_EVENTS);
        }

        public RecursiveFileObserver(String path, int mask) {
            super(path, mask);
            mPath = path;
            mMask = mask;
        }

        Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                String path = "";
                int tid = 0;
                switch (msg.what) {

                    case 10:
                        path = msg.getData().getString("path");
                        tid = msg.getData().getInt("tid");
                        obfile(path, msg.what, tid);
                        Log.v("file", "10+file");
                        break;
                    case 11:
                        path = msg.getData().getString("path");
                        tid = msg.getData().getInt("tid");
                        obfile(path, msg.what, tid);
                        Log.v("file", "11+file");
                        break;
                }
                super.handleMessage(msg);
            }
        };

        @Override
        public void startWatching() {
            if (mObservers != null) return;

            mObservers = new ArrayList<SingleFileObserver>();
            Stack<String> stack = new Stack<String>();
            stack.push(mPath);

            while (!stack.isEmpty()) {
                String parent = stack.pop();
                mObservers.add(new SingleFileObserver(parent, mMask));
                File path = new File(parent);
                File[] files = path.listFiles();
                if (null == files) continue;
                for (File f : files) {
                    if (f.isDirectory() && !f.getName().equals(".") && !f.getName().equals("..")) {
                        stack.push(f.getPath());
                    }
                }
            }

            for (SingleFileObserver sfo : mObservers) {
                sfo.startWatching();
            }
        }

        @Override
        public void stopWatching() {
            if (mObservers == null) return;

            for (SingleFileObserver sfo : mObservers) {
                sfo.stopWatching();
            }
            mObservers.clear();
            mObservers = null;
        }

        void obfile(String path, int work, int tid) {
            Bundle bundle;
            Message msg;
            Log.v("file", "ob" + path);
            AsyncHttpClient httpClient = App.httpClient;
            String url = "";
            if (work == 10) {
                url = App.host + "/home/index/addfile";
            } else if (work == 11) {
                url = App.host + "/home/index/deletefile";
            } else if (work == 3) {
                url = App.host + "/home/index/savefile";
            }
            RequestParams params = new RequestParams();
            params.put("ClientID", App.cid);
            File file = new File(path);
            if (file.exists()) {
                Log.v("file", "exist" + file.getName());
            } else {
                Log.v(" ", "noex");
            }
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File f : files) {
                    bundle = new Bundle();
                    String path1 = f.getPath();
                    bundle.putString("path", path1);
                    if (path1.endsWith(".jpg") ||
                            path1.endsWith(".jpeg")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 1);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path1.endsWith(".doc") ||
                            path1.endsWith(".docx") ||
                            path1.endsWith(".pdf") ||
                            path1.endsWith(".ppt") ||
                            path1.endsWith(".pptx") ||
                            path1.endsWith(".xls") ||
                            path1.endsWith(".xlsx")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 2);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path1.endsWith(".avi") ||
                            path1.endsWith(".rmvb") ||
                            path1.endsWith(".rm") ||
                            path1.endsWith(".mp4") ||
                            path1.endsWith(".wav") ||
                            path1.endsWith(".flv") ||
                            path1.endsWith(".mkv")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                }
            }
            params.put("text", file.getName());
            params.put("state", "open");
            params.put("path", file.getPath());
            params.put("tid", tid);
            httpClient.post(url, params, new TextHttpResponseHandler() {
                @Override
                public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                    Log.v("file", "failue");
                }

                @Override
                public void onSuccess(int i, Header[] headers, String s) {
                    if (i == 200)
                        Log.v("file", "success" + s);
                    else
                        Log.v("file", "fei 200");
                }
            });
        }

        @Override
        public void onEvent(int event, String path) {
            Message msg = new Message();
            Bundle bundle = new Bundle();
            switch (event) {
//                case FileObserver.ACCESS:
//                    Log.i("RecursiveFileObserver", "ACCESS: " + path);
//                    break;
//                case FileObserver.ATTRIB:
//                    Log.i("RecursiveFileObserver", "ATTRIB: " + path);
//                    break;
//                case FileObserver.CLOSE_NOWRITE:
//                    Log.i("RecursiveFileObserver", "CLOSE_NOWRITE: " + path);
//                    break;
//                case FileObserver.CLOSE_WRITE:
//                    Log.i("RecursiveFileObserver", "CLOSE_WRITE: " + path);
//                    break;
                case FileObserver.CREATE:
                    Log.i("file", "CREATE: " + path);
                    bundle = new Bundle();
                    bundle.putString("path", path);
                    if (path.endsWith(".jpg") ||
                            path.endsWith(".jpeg")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 1);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path.endsWith(".doc") ||
                            path.endsWith(".docx") ||
                            path.endsWith(".pdf") ||
                            path.endsWith(".ppt") ||
                            path.endsWith(".pptx") ||
                            path.endsWith(".xls") ||
                            path.endsWith(".xlsx")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 2);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path.endsWith(".avi") ||
                            path.endsWith(".rmvb") ||
                            path.endsWith(".rm") ||
                            path.endsWith(".mp4") ||
                            path.endsWith(".wav") ||
                            path.endsWith(".flv") ||
                            path.endsWith(".mkv")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (new File(path).isDirectory()) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                    break;
                case FileObserver.DELETE:
                    Log.i("file", "DELETE: " + path);
                    bundle = new Bundle();
                    bundle.putString("path", path);
                    if (path.endsWith(".jpg") ||
                            path.endsWith(".jpeg")) {
                        msg = new Message();
                        msg.what = 11;
                        bundle.putInt("tid", 1);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path.endsWith(".doc") ||
                            path.endsWith(".docx") ||
                            path.endsWith(".pdf") ||
                            path.endsWith(".ppt") ||
                            path.endsWith(".pptx") ||
                            path.endsWith(".xls") ||
                            path.endsWith(".xlsx")) {
                        msg = new Message();
                        msg.what = 11;
                        bundle.putInt("tid", 2);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path.endsWith(".avi") ||
                            path.endsWith(".rmvb") ||
                            path.endsWith(".rm") ||
                            path.endsWith(".mp4") ||
                            path.endsWith(".wav") ||
                            path.endsWith(".flv") ||
                            path.endsWith(".mkv")) {
                        msg = new Message();
                        msg.what = 11;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (new File(path).isDirectory()) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                    break;
//                case FileObserver.DELETE_SELF:
//                    Log.i("RecursiveFileObserver", "DELETE_SELF: " + path);
//                    break;
                case FileObserver.MODIFY:
                    //      Log.i("file", "MODIFY: " + path);
                    break;
//                case FileObserver.MOVE_SELF:
//                    Log.i("RecursiveFileObserver", "MOVE_SELF: " + path);
//                    break;
                case FileObserver.MOVED_FROM:
                    Log.i("file", "MOVED_FROM: " + path);
                    Log.i("file", "DELETE: " + path);
                    bundle = new Bundle();
                    bundle.putString("path", path);
                    if (path.endsWith(".jpg") ||
                            path.endsWith(".jpeg")) {
                        msg = new Message();
                        msg.what = 11;
                        bundle.putInt("tid", 1);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path.endsWith(".doc") ||
                            path.endsWith(".docx") ||
                            path.endsWith(".pdf") ||
                            path.endsWith(".ppt") ||
                            path.endsWith(".pptx") ||
                            path.endsWith(".xls") ||
                            path.endsWith(".xlsx")) {
                        msg = new Message();
                        msg.what = 11;
                        bundle.putInt("tid", 2);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path.endsWith(".avi") ||
                            path.endsWith(".rmvb") ||
                            path.endsWith(".rm") ||
                            path.endsWith(".mp4") ||
                            path.endsWith(".wav") ||
                            path.endsWith(".flv") ||
                            path.endsWith(".mkv")) {
                        msg = new Message();
                        msg.what = 11;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (new File(path).isDirectory()) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                    break;
                case FileObserver.MOVED_TO:
                    Log.i("file", "MOVED_TO: " + path);
                    bundle = new Bundle();
                    bundle.putString("path", path);
                    if (path.endsWith(".jpg") ||
                            path.endsWith(".jpeg")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 1);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path.endsWith(".doc") ||
                            path.endsWith(".docx") ||
                            path.endsWith(".pdf") ||
                            path.endsWith(".ppt") ||
                            path.endsWith(".pptx") ||
                            path.endsWith(".xls") ||
                            path.endsWith(".xlsx")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 2);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (path.endsWith(".avi") ||
                            path.endsWith(".rmvb") ||
                            path.endsWith(".rm") ||
                            path.endsWith(".mp4") ||
                            path.endsWith(".wav") ||
                            path.endsWith(".flv") ||
                            path.endsWith(".mkv")) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    } else if (new File(path).isDirectory()) {
                        msg = new Message();
                        msg.what = 10;
                        bundle.putInt("tid", 3);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                    break;
//                case FileObserver.OPEN:
//                    Log.i("RecursiveFileObserver", "OPEN: " + path);
//                    break;
                default:
                    //    Log.i("RecursiveFileObserver", "DEFAULT(" + event + "): " + path);
                    break;
            }
        }

        /**
         * Monitor single directory and dispatch all events to its parent, with full path.
         *
         * @author uestc.Mobius <[email protected]>
         * @version 2011.0121
         */
        class SingleFileObserver extends FileObserver {
            String mPath;

            public SingleFileObserver(String path) {
                this(path, ALL_EVENTS);
                mPath = path;
            }

            public SingleFileObserver(String path, int mask) {
                super(path, mask);
                mPath = path;
            }

            @Override
            public void onEvent(int event, String path) {
                String newPath = mPath + "/" + path;
                RecursiveFileObserver.this.onEvent(event, newPath);
            }
        }
    }

本来fileobserver只能对一个文件夹下面的子文件进行监听。并不能监听子目录的子文件。因此笔者这里实现了文件的递归监听。

在需要监听的地方设置如下

if (null == mFileObserver) {
            mFileObserver = new RecursiveFileObserver("/storage");
            System.out.println("epath" + Environment.getExternalStorageDirectory().getPath());
            Log.v("file", "eeee" + Environment.getExternalStorageDirectory().getPath());
            mFileObserver.startWatching(); //开始监听
        }

这样就实现了文件目录的同步更新。

我是搬运工,热爱我的热爱。

猜你喜欢

转载自blog.csdn.net/u014472643/article/details/52355151