我正在处理一个覆盖问题,我需要在 java 中使用 regEx 验证文件名。在我的应用程序支持 .pdf 、 .txt 、 csv 等。我的文件名从 user 获取为 xxx.txt。我想使用正确的扩展名格式验证我的文件名,并且不包含点以外的任何特殊字符(例如 .txt)。
filePath = properties.getProperty("DOCUMENT.LIBRARY.LOCATION");
String fileName = (String) request.getParameter("read");
只有当文件路径完成其正确的验证时,下面的代码才应该有效。
filePath += "/" + fileName;
if (fileName.endsWith(".pdf")) {
// do something
}
或者使用方法 String.matches()
fileName.matches("\\.(pdf|txt|csv)$")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句