filevalidator.java

来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 59 行

JAVA
59
字号
package firewall.common;/** * Title:        Professional Java Servlet Programming - Chapter 2 * Description:  Class to provide common static utility methods * Copyright:    Copyright (c) 2001 * Company: * @author Andrew Harbourne-Thomas * @version 1.0 */public class FileValidator{  /**   * Provides a (limited) String array of valid   *  character file extensions   */  public static final String[] VALID_EXTENSIONS =              {"txt", "html", "htm", "java"};  /**   * This method is desinged to determine if the filename passed   * is valid according to the following rules:   *  filename cannot be null or empty,   *  filename must end in a valid extension (as specified)   *   * @param filename - the filename to be checked   * @return boolean indication if the filename is valid;   *          true if it is, false otherwise.   */  public static boolean isFileNameValid(String filename)  {    if (filename == null || filename.length() == 0) {      return false;    }    for (int i = 0; i < VALID_EXTENSIONS.length; i++)    {      if (filename.endsWith("." + VALID_EXTENSIONS[i])) {        return true;      }    }    return false;  }  /**   * This method recieves a file list (String[]) and checks that all   * files are valid names.  If not, it removes them (replaces with null).   */  public static void cleanFileList(String[] files)  {    for (int i = 0; i < files.length; i++)    {      if (!isFileNameValid(files[i])) {        files[i] = null;      }    }  }}

⌨️ 快捷键说明

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