java8解码以base64编码的图像

时间:2015-09-17 17:45:12

标签: java base64 decode

我正在尝试使用java.util.Base64(java8)解码用base64字符串编码的图像。

这是base64编码的字符串:

/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

可以在http://codebeautify.org/base64-to-image-converter

上解码



当我尝试将base64字符串解码为byte时。我收到此错误消息:

java.lang.IllegalArgumentException: Illegal base64 character 20


这是我的代码:

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);

我不知道出了什么问题。请帮助,谢谢

3 个答案:

答案 0 :(得分:16)

您的编码字符串中有一个空格,这是base64中的非法字符。如果删除它,它仍会在链接到的转换器中生成相同的图像,现在也可以通过代码进行解码。

<强>更新

某些解码器(如您链接的解码器或Java中的Base64.getMimeDecoder())会忽略非法字符,而其他解码器(如Base64.getDecoder())则不允许使用它们。

答案 1 :(得分:0)

当我通过@RequestParam接收到base64字符串时,遇到了相同的问题,它将“ +”接收为“”。

Base64.getDecoder().decode()给了我同样的异常(java.lang.IllegalArgumentException:非法的base64字符20) 和Base64.getMimeDecoder()只是忽略了空格,并以错误保存了图像。

修复:

public byte[] convertImage(String base64) { String base64Image = base64.replaceAll(" ", "+"); return byte[] imageBytes = java.util.Base64.getDecoder().decode(base64Image); }

对我有用。

答案 2 :(得分:0)

是的,它也对我有用。非常感谢。

 String base64Image = base64.replaceAll(" ", "+");

查询-我会说这是临时解决方案,因为在加密的字符串中,如果为空或任何其他字符,将无法使用。