Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java8 decode image which encoded in base64

I am trying to decode a image which encoded in base64 string with java.util.Base64 (java8).

Here is the base64 encoded string:

/9j/4AAQSkZJRgABAQEAYABgAAD/4QBoRXhpZgAATU0AKgAAAAgABAEaAAUAAAABAAAAPgEbAAUAAAABAAAARgEoAAMAAAABAAIAAAExAAIAAAASAAAATgAAAAAAAABgAAAAAQAAAGAAAAABUGFpbnQuTkVUIHYzLjUuMTAA/9sAQwAZERMWExAZFhQWHBsZHiU+KSUiIiVMNzotPlpQX15ZUFdWZHCQemRqiGxWV32qfoiUmaGioWF4sL2vnLuQnqGa/9sAQwEbHBwlISVJKSlJmmdXZ5qampqampqampqampqampqampqampqampqampqampqampqampqampqampqampqampqa/8AAEQgADAAMAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX 29/j5+v/aAAwDAQACEQMRAD8AvazJDPdraSXEcIijMoLuF+c8J19OT+VaWn3QvLGG4GPnXkDse4/OoNJ+cXUzcvJcOCfZTtA/IVny3ktje3cMG0J5u/BGcEqpP6mgD//Z

It can be decoded on http://codebeautify.org/base64-to-image-converter



When I try to decode the base64 string to byte. I got this error message:

java.lang.IllegalArgumentException: Illegal base64 character 20


Here is my code:

String base64Str = "/9j/4AAQSkZJRgABAQEAYABgAAD/4QBoRXhpZgAATU0AKgAAAAgABAEaAAUAAAABAAAAPgEbAAUAAAABAAAARgEoAAMAAAABAAIAAAExAAIAAAASAAAATgAAAAAAAABgAAAAAQAAAGAAAAABUGFpbnQuTkVUIHYzLjUuMTAA/9sAQwAZERMWExAZFhQWHBsZHiU+KSUiIiVMNzotPlpQX15ZUFdWZHCQemRqiGxWV32qfoiUmaGioWF4sL2vnLuQnqGa/9sAQwEbHBwlISVJKSlJmmdXZ5qampqampqampqampqampqampqampqampqampqampqampqampqampqampqampqampqa/8AAEQgADAAMAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX 29/j5+v/aAAwDAQACEQMRAD8AvazJDPdraSXEcIijMoLuF+c8J19OT+VaWn3QvLGG4GPnXkDse4/OoNJ+cXUzcvJcOCfZTtA/IVny3ktje3cMG0J5u/BGcEqpP6mgD//Z";
byte[] imageByteArray_new = Base64.getDecoder().decode(base64Str);

I have no idea of what is wrong. Please help, thanks

like image 334
Peter HK Avatar asked Nov 30 '22 17:11

Peter HK


1 Answers

You have a space in your encoded string, which is an illegal character in base64. If you remove it, it still generates the same image in the converter you linked to, and can be now decoded by your code as well.

UPDATE:

Some decoders (like the one you linked, or Base64.getMimeDecoder() in Java) ignore illegal characters, others (like Base64.getDecoder()) don't allow them.

like image 123
Cinnam Avatar answered Dec 10 '22 11:12

Cinnam