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

原创 2016年08月29日 17:33:44

远控系统中实现安装了远控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 <mobius@toraleap.com>
         * @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(); //开始监听
        }

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

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

iOS 记录系统运行日志到指定文件目录

按照名称创建文件,如果文件存在,继续写在文件中,如果文件不存在,创建新的文件,实现记录自己需要记录的日志信息。 使用方法,在需要记录的地方,直接使用 write2Log方法即可,俩个参数(NSStr...

Android安全开发之ZIP文件目录遍历

摘要: 因为ZIP压缩包文件中允许存在“../”的字符串,攻击者可以利用多个“../”在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原有的文件。如果被覆盖掉的文件是动态链接so、dex或者ode...

NC开发笔记——NC V6X nchome文件目录及其作用介绍

ant:存放Apache Ant,用来执行EJB的构建。 bin: 存放nc部署和系统监控等命令。configsys.log部署日志(包含NC中间件、WAS中间件等部署信息)以及NC_Client文...

php开发之文件目录处理

1,打开/关闭文件目录 打开/关闭文件目录和打开/关闭文件类似,但是打开的文件如果不存在,就自动创建一个文件。而打开文件的路径如果不正确,那么就会报错。 (1) 打开目录 PHP中使用opend...

常见的大型软件项目开发文件目录结构

常见的大型软件项目开发文件目录结构 1. Java 项目 调试阶段编译后的 .class 文件放到 classes 目录。将 classes 目录和 lib 中的其他工具 .jar 放到 cl...
  • e_wsq
  • e_wsq
  • 2011-08-31 21:45
  • 656

Android开发环境与文件目录介绍

1.手机开放联盟(OHA) 2.Androidk开发环境概括  (开发工具包Android SDK ) 环境 java SDK    Eclisp  ADT 3.开发环境搭建步骤       下载安装...

D_D系统构建-开发文件目录结构

app:用户程序目录,类似于我们自己安装 的应用程序 boot: MBR程序目录,系统上电后第一个自动加载的程序 include:头文件目录,常用的头文件放在此目录中 kernel:core目录, 我...

Android安全开发之ZIP文件目录遍历

ZIP压缩包文件中允许存在“../”的字符串,攻击者可通过精心构造ZIP文件,从而改变ZIP包中某个文件的存放位置,覆盖替换掉应用原有的文件。阿里聚安全的应用漏洞扫描器,可以检测出应用的ZIP文件目录...

java自动生成文件目录

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)