Base64: java.lang.IllegalArgumentException: Illegal character
Your encoded text is [B@6499375d
. That is not Base64, something went wrong while encoding. That decoding code looks good.
Use this code to convert the byte[] to a String before adding it to the URL:
String encodedEmailString = new String(encodedEmail, "UTF-8");
// ...
String confirmLink = "Complete your registration by clicking on following"
+ "\n<a href='" + confirmationURL + encodedEmailString + "'>link</a>";
I encountered this error since my encoded image started with data:image/png;base64,iVBORw0...
.
This answer led me to the solution:
String partSeparator = ",";
if (data.contains(partSeparator)) {
String encodedImg = data.split(partSeparator)[1];
byte[] decodedImg = Base64.getDecoder().decode(encodedImg.getBytes(StandardCharsets.UTF_8));
Path destinationFile = Paths.get("/path/to/imageDir", "myImage.png");
Files.write(destinationFile, decodedImg);
}
That code removes the meta data in front of the Base64-encoded image and passes the Base64 string to Java's Base64.Decoder
to get the image as bytes.