Base64报错:Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
原因:Base64不支持中文转码
解决方案:先进行url转码,再进行Base64转码
转换过程示意:
str --> uriStr --> uriBase64Str --> uriStr --> str
//前台转码
//url 转码
userAccount = encodeURI(userAccount);
//Base64 转码
userAccount = window.btoa(userAccount);
//后台解码
//Base64 解码
java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
userAccount = new String(decoder.decode(userAccount));
//url 解码
userAccount = URLDecoder.decode(userAccount, "UTF-8");