I have Query parameter that was encoded by btoa
javascript function. when url clicked, request function called inside java controller where i wanted to decode Query parameter (which is encoded from javascript btoa). i have tried BASE64Decoder
and Base64.getDecoder()
but not able to get proper value. is there any other way to do so?
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);
Example
String to encode : [email protected]
Encoded String : ZGVtb0BkZW1vLmNvbQ==
Java 8 has a new Base64 package:
public void test() {
String s = "[email protected]";
String encoded = new String(Base64.getEncoder().encode(s.getBytes()));
String decoded = new String(Base64.getDecoder().decode(encoded));
System.out.println("S: " + s + " -> " + encoded + " -> " + decoded);
}
prints
S: [email protected] -> ZGVtb0BkZW1vLmNvbQ== -> [email protected]
There are also other encoder/decoder pairs - you may find the mime encoder appropriate to your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With