Merge pull request '修复产品库移入制品时若制品为中文会被urlEncode两次导致中文无法正常显示' (#809) from otto/microservices:product_refact into product_refact

This commit is contained in:
otto 2025-01-22 11:33:29 +08:00
commit 5b8524936a
1 changed files with 21 additions and 1 deletions

View File

@ -228,7 +228,11 @@ public class ServletUtils {
StringBuilder stringBuffer = new StringBuilder();
String[] strings = str.split("/");
for (String string : strings) {
stringBuffer.append("/").append(URLEncoder.encode(string, Constants.UTF8));
// 如果url已经进行过urlEncode 不重复进行Encode
if (!isUrlEncoded(string)) {
string = URLEncoder.encode(string, Constants.UTF8);
}
stringBuffer.append("/").append(string);
}
return stringBuffer.toString().replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
@ -236,6 +240,22 @@ public class ServletUtils {
}
}
/**
* 判断字符串是否经过URLEncode
* 1. 使用URLDecoder.decode对字符串进行解码
* 2. 再使用URLEncoder.encode对解码后的字符串重新编码
* 3. 如果重新编码后的字符串与原始字符串相同则说明原始字符串是经过URL编码的
*
* @param encodedString 需encode的字符串
* @return 是否经过URLEncode
* @throws UnsupportedEncodingException 编码异常
*/
public static boolean isUrlEncoded(String encodedString) throws UnsupportedEncodingException {
String decodedString = URLDecoder.decode(encodedString, "UTF-8");
String reencodedString = URLEncoder.encode(decodedString, "UTF-8");
return encodedString.equalsIgnoreCase(reencodedString);
}
/**
* 内容解码
*