特殊符号在url路径中不允许出现,上传的时候可以将特殊符号替换掉。
/**
* 检查是否存在特殊符号
*
* @param str
* @return
* @throws PatternSyntaxException
*/
public static boolean checkSpecialChar(String str) throws PatternSyntaxException {
// 清除掉所有特殊字符
String regEx = ".*[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]+.*";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.matches();
}
/**
* 替换特殊符合,默认替换为-
*
* @param str
* @return
* @throws PatternSyntaxException
*/
public static String filterString(String str) throws PatternSyntaxException {
return filterString(str, "-");
}
/**
* 替换特殊符合
*
* @param str
* @param replacement 代替的值
* @return
* @throws PatternSyntaxException
*/
public static String filterString(String str, String replacement) throws PatternSyntaxException {
String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll(replacement).trim();
}