我正在尝试使用ESAPI提供的jar(esapi-2.0_rc11)的DefaultValidator类的getValidFileName(字符串,字符串,列表,布尔值)方法来验证文件名。但是在运行时却没有此类方法异常。

这是我的代码:

public static String getValidFileName(String input,String[] strFileExtns, Boolean isNullable) throws Exception
{
    List <String> fileExtnsList = new ArrayList <String>();

if (strFileExtns != null && strFileExtns.length > 0)
    for(int i=0; i<strFileExtns.length; i++)
    fileExtnsList.add(strFileExtns[i]);

    return new DefaultValidator().getValidFileName("FileNameValidation", input, fileExtnsList, isNullable);
}


我正进入(状态
java.lang.NoSuchMethodError:org/owasp/esapi/reference/DefaultValidator.getValidFileName(Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Z)Ljava/lang/String;

jar中存在的代码:

public String getValidFileName(String context, String input, List<String> allowedExtensions, boolean allowNull)
    throws ValidationException, IntrusionException
  {
    if ((allowedExtensions == null) || (allowedExtensions.isEmpty())) {
      throw new ValidationException("Internal Error", "getValidFileName called with an empty or null list of allowed Extensions, therefore no files can be uploaded");
    }

    String canonical = "";
    try
    {
      if (isEmpty(input)) {
        if (allowNull) return null;
        throw new ValidationException(context + ": Input file name required", "Input required: context=" + context + ", input=" + input, context);
      }

      canonical = new File(input).getCanonicalFile().getName();
      getValidInput(context, input, "FileName", 255, true);

      File f = new File(canonical);
      String c = f.getCanonicalPath();
      String cpath = c.substring(c.lastIndexOf(File.separator) + 1);

      if (!(input.equals(cpath)))
        throw new ValidationException(context + ": Invalid file name", "Invalid directory name does not match the canonical path: context=" + context + ", input=" + input + ", canonical=" + canonical, context);
    }
    catch (IOException e)
    {
      throw new ValidationException(context + ": Invalid file name", "Invalid file name does not exist: context=" + context + ", canonical=" + canonical, e, context);
    }

    Iterator i = allowedExtensions.iterator();
    while (i.hasNext()) {
      String ext = (String)i.next();
      if (input.toLowerCase().endsWith(ext.toLowerCase()))
        return canonical;
    }

    throw new ValidationException(context + ": Invalid file name does not have valid extension ( " + allowedExtensions + ")", "Invalid file name does not have valid extension ( " + allowedExtensions + "): context=" + context + ", input=" + input, context);
  }


有人请帮我。

最佳答案

java.lang.NoSuchMethodError错误通常是由依赖性问题引起的。如果您使用的是maven(我可能会这样,因为经常会发生此错误),请按以下方式解决错误:

尝试在命令行上发出“ MVN依赖项:tree -Dverbose”,并检查包含org / owasp / esapi / reference / DefaultValidator的库是否为您想要的版本。如果不是,则可以使用exclusions标记从包含错误版本的依赖项中排除错误版本。

还要检查您生成的类路径是否以正确的顺序列出了依赖项。

关于java - 调用DefaultValidator.getValidFileName()时出现NoSuchMethoException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14351705/

10-10 16:24