gpt4 book ai didi

java - 调用 DefaultValidator.getValidFileName() 时出现 NoSuchMethoException

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:43 24 4
gpt4 key购买 nike

我尝试使用 ESAPI 提供的 jar (esapi-2.0_rc11) 中的 DefaultValidator 类的 getValidFileName (String, String, list, boolean) 方法来验证文件名。但在运行时出现 No such method 异常。

这是我的代码:

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 dependency:tree -Dverbose”,并检查包含 org/owasp/esapi/reference/DefaultValidator 的库是否是您想要的版本。如果没有,您可以使用排除标记从包含错误版本的依赖项中排除错误版本。

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com