我有一个由 btoa
javascript 函数编码的查询参数。单击 url 时,请求函数在 java Controller 内部调用,我想在其中解码查询参数(从 javascript btoa 编码)。我已经尝试过 BASE64Decoder
和 Base64.getDecoder()
但无法获得正确的值。还有其他方法吗?
Java Controller
@RequestMapping(value = "decode/{email}", method = RequestMethod.GET)
public String decodeEmail(Model model, @PathVariable String email){
Decode decode = new Decode();
decode.setEmail(email);
decodeService.save(decode);
return "decode/List";
}
JavaScript
var email = document.getElementById("email").value;
var encodedEmail = btoa(email);
示例
要编码的字符串:demo@demo.com
编码字符串:ZGVtb0BkZW1vLmNvbQ==
最佳答案
Java 8 有一个新的 Base64包裹:
public void test() {
String s = "demo@demo.com";
String encoded = new String(Base64.getEncoder().encode(s.getBytes()));
String decoded = new String(Base64.getDecoder().decode(encoded));
System.out.println("S: " + s + " -> " + encoded + " -> " + decoded);
}
打印
S: demo@demo.com -> ZGVtb0BkZW1vLmNvbQ== -> demo@demo.com
还有其他编码器/解码器对 - 您可能会发现 mime encoder适合您的需求。
关于javascript - 类似于 javascript 中的 atob 的 Java 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37674016/