java.lang.IllegalArgumentException:非法的base64字符a

问题描述 投票:0回答:2

我有这个字符串数据要进行 Base64 解码

String mfstr = "BZCaRm9ChAbA58sgSwlhzgVuPwboh2qvgcPuLxKJkLpesdHvyZtaheThUSw6%2BHItGBtgimHpXqbn%0ApggBaRR2wisjNiyQrX03eEJlet6%2BqFL6TouRr0wW3NLRZSHOHUSFtJkpq0cyXy%2FSfMVB47y93xlq%0Az845uXSTK2Vi%2FgzwFVphHd%2BTK%2FrO%2FDxJ4EfvAoW0zxeYS%2BCWnIsl%2F4ILehVYasGtxC%2FjbG1I8S%2Fc%0AZoqXIcPmPWrszbG7R1ouDQ473TyCMLx9PBsl1Z%2Bj39V4Qr01ZRw7GVP2m%2Bk4xrHg2Im1OuXpd2vl%0AKGwe5j2T1ZHtoYxCvXOOU1YeJYSR%2Ff7Kd7KbpnjvFT2Ua%2FOdHx%2FKzBoK3Yk97fdvMcelUGMxveKq%0A8C9aCXFVU1xjK81CwB72QWkK5%2B8DCjItVDFcpnVFnhk8ZwlYKU6o8jETDockNMKiDmBYqKGpnNII%0ACnQBGiWy0inWj40k8VoFNIuVK1yYzLoVvFrYR514Ex6U2AK00c0f7C2C5vISsOEp%2BW8KHG2hFW7G%0A97IgwnX3vtQc0s0SaZ%2B7SPgbxwUujlULaOa0t5W9ZDs9b7jDyBZA7m8DiFrLH2YpzpBhrHXcw%2B%2BZ%0A";

String carg = URLDecoder.decode(mfstr, "UTF-8");

byte[] v5 = Base64.getDecoder().decode(carg.getBytes());

当我运行此代码时,出现错误:

java.lang.IllegalArgumentException: Illegal base64 character a
,到目前为止,我已尝试在
StandardCharsets.UTF_8
中传递
carg.getBytes()
但什么也没有。我在这里做错了什么?

java encoding character-encoding base64 urlencode
2个回答
11
投票

答案只是找到一种方法来删除这些换行符,因为它们不是有效的

base64
字符。我将代码更改如下:

String mfstr = "BZCaRm9ChAbA58sgSwlhzgVuPwboh2qvgcPuLxKJkLpesdHvyZtaheThUSw6%2BHItGBtgimHpXqbn%0ApggBaRR2wisjNiyQrX03eEJlet6%2BqFL6TouRr0wW3NLRZSHOHUSFtJkpq0cyXy%2FSfMVB47y93xlq%0Az845uXSTK2Vi%2FgzwFVphHd%2BTK%2FrO%2FDxJ4EfvAoW0zxeYS%2BCWnIsl%2F4ILehVYasGtxC%2FjbG1I8S%2Fc%0AZoqXIcPmPWrszbG7R1ouDQ473TyCMLx9PBsl1Z%2Bj39V4Qr01ZRw7GVP2m%2Bk4xrHg2Im1OuXpd2vl%0AKGwe5j2T1ZHtoYxCvXOOU1YeJYSR%2Ff7Kd7KbpnjvFT2Ua%2FOdHx%2FKzBoK3Yk97fdvMcelUGMxveKq%0A8C9aCXFVU1xjK81CwB72QWkK5%2B8DCjItVDFcpnVFnhk8ZwlYKU6o8jETDockNMKiDmBYqKGpnNII%0ACnQBGiWy0inWj40k8VoFNIuVK1yYzLoVvFrYR514Ex6U2AK00c0f7C2C5vISsOEp%2BW8KHG2hFW7G%0A97IgwnX3vtQc0s0SaZ%2B7SPgbxwUujlULaOa0t5W9ZDs9b7jDyBZA7m8DiFrLH2YpzpBhrHXcw%2B%2BZ%0A";

String carg = URLDecoder.decode(mfstr, "UTF-8");

carg = carg.replace("\n","");

byte[] v5 = Base64.getDecoder().decode(carg.getBytes());

感谢@Tom 的讨论。


0
投票

检查您是否正在尝试解码编码的字符串。当您尝试在代码中的以下位置解码纯文本时,通常会发生这种情况

Base64.getDecoder().decode(encodedText);
© www.soinside.com 2019 - 2024. All rights reserved.