⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 virtualdirectory.java

📁 Ftp服务1.0
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }
        }
        return true;
    }
    
    
    /**
     * Print file list.
     * <pre>
     *   -l : detail listing
     *   -a : display all (including hidden files)
     * </pre>
     * @return true if success
     */
    public boolean printNList(String argument, Writer out) throws IOException {
        
        String lsDirName = "./";
        String options = "";
        String pattern   = "*";

        // get options, directory name and pattern
        if(argument != null) {
            argument = argument.trim();
            StringBuffer optionsSb = new StringBuffer(4);
            StringTokenizer st = new StringTokenizer(argument, " ");
            while(st.hasMoreTokens()) {
                String token = st.nextToken();
                if(token.charAt(0) == '-') {
                    if (token.length() > 1) {
                        optionsSb.append(token.substring(1));
                    }
                }
                else {
                   lsDirName = token;
                }
            }
            options = optionsSb.toString();
        }

        // check options
        boolean bAll = options.indexOf('a') != -1;
        boolean bDetail = options.indexOf('l') != -1;

        // check pattern
        lsDirName = getPhysicalName(lsDirName);
        int slashIndex = lsDirName.lastIndexOf('/');
        if( (slashIndex != -1) && (slashIndex != (lsDirName.length() -1)) ) {
            pattern = lsDirName.substring(slashIndex+1);
            lsDirName = lsDirName.substring(0, slashIndex+1);
        }

        // check directory
        File lstDirObj = new File(lsDirName);
        if(!lstDirObj.exists()) {
            return false;
        }
        if(!lstDirObj.isDirectory()) {
            return false;
        }

        // get file list
        File flLst[];
        if ( (pattern == null) || pattern.equals("*") || pattern.equals("") ) {
            flLst = lstDirObj.listFiles();
        }
        else {
            flLst = lstDirObj.listFiles(new FileRegularFilter(pattern));
        }
        
        // print file list
        if (flLst != null) {
            for(int i=0; i<flLst.length; i++) {
                if ( (!bAll) && flLst[i].isHidden() ) {
                    continue;
                }
                if(bDetail) {
                    printLine(flLst[i], out);
                }
                else {
                    out.write(getName(flLst[i]));
                }
                out.write(NEWLINE);
            }
        }
        return true;
    }
    
    /**
     * Get file owner.
     */
    private String getOwner(File fl) {
        return "user";
    }


    /**
     * Get group name
     */
    private String getGroup(File fl) {
        return "group";
    }
    
    
    /**
     * Get link count
     */
    private String getLinkCount(File fl) {
        if(fl.isDirectory()) {
            return String.valueOf(3);
        }
        else {
            return String.valueOf(1);
        }
    }
    
    
    /**
     * Get size
     */
    private String getLength(File fl) {
        String initStr = "            ";
        long sz = 0;
        if(fl.isFile()) {
            sz = fl.length();
        }
        String szStr = String.valueOf(sz);
        if(szStr.length() > initStr.length()) {
            return szStr;
        }
        return initStr.substring(0, initStr.length() - szStr.length()) + szStr;
    }


    /**
     * Get last modified date string.
     */
    private String getLastModified(File fl) {
        long modTime = fl.lastModified();
        Date date = new Date(modTime);
        return DateUtils.getUnixDate(date);
    }

    /**
     * Get file name.
     */
    private String getName(File fl) {
        String flName = fl.getName();
        flName = normalizeSeparateChar(flName);

        int lastIndex = flName.lastIndexOf("/");
        if(lastIndex == -1) {
            return flName;
        }
        else {
            return flName.substring(lastIndex + 1);
        }
    }
   
    
    /**
     * Get permission string.
     */
    private String getPermission(File fl) {

        StringBuffer sb = new StringBuffer(13);
        if(fl.isDirectory()) {
            sb.append('d');
        }
        else {
            sb.append('-');
        }
        
        if (fl.canRead()) {
            sb.append('r');
        }
        else {
            sb.append('-');
        }
        
        if (mbWritePerm && fl.canWrite()) {
            sb.append('w');
        }
        else {
            sb.append('-');
        }
        sb.append("-------");
        return sb.toString();
    }
    
    
    /**
     * Normalize separate characher. Separate character should be '/' always.
     */
    private String normalizeSeparateChar(String pathName) {
       pathName = pathName.replace(File.separatorChar, '/');
       pathName = pathName.replace('\\', '/');
       return pathName;
    }
    
    
    /**
     * Replace dots. Returns physical name.
     * @param inArg the virtaul name
     */
    private String replaceDots(String inArg) {
        
        // get the starting directory
        String resArg;
        if(inArg.charAt(0) != '/') {
            resArg = mstRoot + mstCurrDir.substring(1);
        }
        else {
            resArg = mstRoot;
        }     
        
        // strip last '/'   
        if(resArg.charAt(resArg.length() -1) == '/') {
            resArg = resArg.substring(0, resArg.length()-1);
        }
        
        // replace ., ~ and ..        
        StringTokenizer st = new StringTokenizer(inArg, "/");
        while(st.hasMoreTokens()) {

            String tok = st.nextToken().trim();

            // . => current directory
            if(tok.equals(".")) {
                continue;
            }

            // .. => parent directory (if not root)
            if(tok.equals("..")) {
                if(resArg.startsWith(mstRoot)) {
                  int slashIndex = resArg.lastIndexOf('/');
                  if(slashIndex != -1) {
                    resArg = resArg.substring(0, slashIndex);
                  }
                }
                continue;
            }
            
            // ~ => home directory (in this case /)
            if (tok.equals("~")) {
                resArg = mstRoot.substring(0, mstRoot.length()-1).trim();
                continue;
            }
            
            resArg = resArg + '/' + tok;
        }
        
        // add last slash if necessary
        if( !inArg.equals("") && (inArg.charAt(inArg.length()-1)=='/') ) {
            resArg = resArg + '/';
        }
        
        // final check
        if (resArg.length() < mstRoot.length()) {
            resArg = mstRoot;
        }
        
        return resArg;
    }
    
    
    /**
     * Get each directory line.
     */
    private void printLine(File fl, Writer out) throws IOException {
        out.write(getPermission(fl));
        out.write(DELIM);
        out.write(DELIM);
        out.write(DELIM);
        out.write(getLinkCount(fl));
        out.write(DELIM);
        out.write(getOwner(fl));
        out.write(DELIM);
        out.write(getGroup(fl));
        out.write(DELIM);
        out.write(getLength(fl));
        out.write(DELIM);
        out.write(getLastModified(fl));
        out.write(DELIM);
        out.write(getName(fl));
    }
    
    /**
     * If the string is not '/', remove last slash.
     */
    private String removeLastSlash(String str) {
        if ( (str.length()>1) && (str.charAt(str.length()-1)=='/') ) {
            str = str.substring(0, str.length()-1);
        }
        return str;
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -