【第22期】观点:IT 行业加班,到底有没有价值?

远控开发记录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(); //开始监听
        }

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

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

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

相关文章推荐

转载_linux内核分析(某位大牛的文章)

启动     当PC启动时,Intel系列的CPU首先进入的是实模式,并开始执行位于地址0xFFFF0处的代码,也就是ROM-BIOS起始位置的代码。BIOS先进行一系列的系统自检,然后初始...

学习文章

原文地址:http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html#00-Foreword01 缘起 其实这两年为 Nginx 世界做了这么多的事情,一直想通过一系列教程性的文章把我的那些工作成果和所学所知都介绍给更多的朋友。现在终于下决心在新浪博客 http://blog.sina.com.cn/openresty 上面用中文写点东西,每一篇东西都会有一个小主题,但次序和组织上就不那么讲究了,毕竟并不是一本完整的图书,或许未来我会将之整理出书也不一定。 我现在编写的教程是按所谓的“系列”来划分的,比如首先连载

(转)android源码大放送(实战开发必备),免费安卓demo源码,例子大全文件详细列表

(转)android源码大放送(实战开发必备),免费安卓demo源码,例子大全文件详细列表 转载地址:点击打开链接
  • lnn368
  • lnn368
  • 2016-04-21 00:47
  • 6698

Android开发项目中文件目录结构

在Android开发中,总的目录结构为如图所示。在src下,我们可以定义不同的包名称,来区分各个类的功能。但在res目录下的文件夹,名称是固定的不能随意更改。

谢旭升《操作系统教程》课后习题解答

原文和下载地址:http://liuyanzhao.com/2916.html习题一1.设计操作系统的主要目的是什么? 设计操作系统的目的是: (1)从系统管理人员的观点来看,设计操作系统是为了合...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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