读文件目录结构部分代码


    public boolean listRoots(String specialId, String specialUrl)
            throws ToolException {

        specialManagerDao.deleteSpecialStoreAndResourse(specialId);

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        uploadDate = dateFormat.format(now);

        File rootFile = new File(specialUrl);
        if (!rootFile.exists()) { // 用户所给的地址不存在
            return false;
        } else {
            if (rootFile.isFile()) {
                String name = rootFile.getName();
                String storePath = rootFile.getAbsolutePath();
                String resourceSuf = name.substring(name.lastIndexOf('.') + 1);
                totalFileNum++;

                // 取文件的目录,添加到专辑资源目录结构中
                String specialStoreId = specialManagerDao.insertspecialStore(
                        specialId, name, 1, "1", 1);
                String resourcesize = String.valueOf(rootFile.length());

                // 将文件信息添加到资源基本信息表中
                specialManagerDao.insertSpecialResource(name, specialId,
                        storePath, resourceSuf, uploadDate, specialStoreId,resourcesize);

            } else {

                // 创建最根目录,添加到专辑资源目录中,
                // grade=1;storeid=000000000001;upnode=0;orderdisplay=1;
                String name = rootFile.getName();
                String specialStoreId = specialManagerDao.insertspecialStore(
                        specialId, name, 1, "1", 1);

                // 获取所有根目录下的所有文件夹和文件信息 int orderdisplay=0; specialUrl
                listDirectory(rootFile, specialStoreId, 1, specialId);

            }

            specialManagerDao.updateSpecialAmountById(totalFileNum, specialId);
            return true;
        }
    }

    private void listDirectory(File dir, String upnode, int upnodeGrade,
            String specialId) throws ToolException {

        int thisGrade = upnodeGrade + 1;

        if (!dir.exists()) {
            System.out.println("文件名称不存在!");
        } else {

            File[] files = dir.listFiles();
            if (files != null) {
                int orderDisplay = 0;
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) { // 文件夹
                        orderDisplay++;

                        String name = files[i].getName();

                        // 创建本目录信息 orderdisplay=j++; grade = upgrade+1; upnode=
                        // upnode
                        String specialStoreId = specialManagerDao
                                .insertspecialStore(specialId, name, thisGrade,
                                        upnode, orderDisplay);

                        listDirectory(files[i], specialStoreId, thisGrade,
                                specialId);
                    } else { // 文件
                        String name = files[i].getName();
                        String storePath = files[i].getAbsolutePath();
                        String resourceSuf = name.substring(name
                                .lastIndexOf('.') + 1);
                        String resourcesize = String.valueOf(files[i].length());
                       
                        specialManagerDao.insertSpecialResource(name,
                                specialId, storePath, resourceSuf, uploadDate,
                                upnode,resourcesize);
                        totalFileNum++;
                    }
                }
            }
        }
    }

    public boolean listVisualRoots(String specialId, String specialUrl)
            throws ToolException {

        specialManagerDao.deleteSpecialStoreAndResourse(specialId);

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        uploadDate = dateFormat.format(now);
       
        String rootPath = ServletActionContext.getServletContext().getRealPath(specialUrl);
       
        File rootFile = new File(rootPath);
        if (!rootFile.exists()) { // 用户所给的地址不存在
            return false;
        } else {
            if (rootFile.isFile()) {
                String name = rootFile.getName();
                String storePath = specialUrl+"/"+name;
                String resourceSuf = name.substring(name.lastIndexOf('.') + 1);
                totalFileNum++;

                // 取文件的目录,添加到专辑资源目录结构中
                String specialStoreId = specialManagerDao.insertspecialStore(
                        specialId, name, 1, "1", 1);
                String resourcesize = String.valueOf(rootFile.length());

                // 将文件信息添加到资源基本信息表中
                specialManagerDao.insertSpecialResource(name, specialId,
                        storePath, resourceSuf, uploadDate, specialStoreId,resourcesize);

            } else {

                // 创建最根目录,添加到专辑资源目录中,
                // grade=1;storeid=000000000001;upnode=0;orderdisplay=1;
                String name = rootFile.getName();
                String specialStoreId = specialManagerDao.insertspecialStore(
                        specialId, name, 1, "1", 1);

                // 获取所有根目录下的所有文件夹和文件信息 int orderdisplay=0; specialUrl
                listVisualDirectory(rootFile, specialStoreId, 1, specialId,specialUrl);

            }

            specialManagerDao.updateSpecialAmountById(totalFileNum, specialId);
            return true;
        }
    }

    private void listVisualDirectory(File dir, String upnode, int upnodeGrade,
            String specialId,String parentVisualPath) throws ToolException {

        int thisGrade = upnodeGrade + 1;

        if (!dir.exists()) {
            System.out.println("文件名称不存在!");
        } else {

            File[] files = dir.listFiles();
            if (files != null) {
                int orderDisplay = 0;
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) { // 文件夹
                        orderDisplay++;

                        String name = files[i].getName();
                        String thisPath = parentVisualPath+"/"+name;

                        // 创建本目录信息 orderdisplay=j++; grade = upgrade+1; upnode=
                        // upnode
                        String specialStoreId = specialManagerDao
                                .insertspecialStore(specialId, name, thisGrade,
                                        upnode, orderDisplay);

                        listVisualDirectory(files[i], specialStoreId, thisGrade,
                                specialId,thisPath);
                    } else { // 文件
                        String name = files[i].getName();
                        String storePath = parentVisualPath+"/"+name;
                        String resourceSuf = name.substring(name
                                .lastIndexOf('.') + 1);
                        String resourcesize = String.valueOf(files[i].length());
                        specialManagerDao.insertSpecialResource(name,
                                specialId, storePath, resourceSuf, uploadDate,
                                upnode,resourcesize);
                        totalFileNum++;
                    }
                }
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值