Base64 解码decode遇到IllegalArgumentException: Illegal base64 character 20
Posted zhengwangzw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Base64 解码decode遇到IllegalArgumentException: Illegal base64 character 20相关的知识,希望对你有一定的参考价值。
base64字符串内容:eyJjb2RlIjoxMDAwMDAsImRhdGEiOnsiZGF0YSI6eyJydWxlIjp7ImRhZXhpbmtlcnVsZSI6IjAu MTAwMTAwMCIsImxhb2tlcnVsZSI6IjAuMDAiLCJyZWplY3RfcnVsZTFfYXQyMDE5MTEiOjAsInVw Z3JhZGVfcnVsZTFfYXQyMDE5MTEiOjAsInVwZ3JhZGVfcnVsZTJfYXQyMDE5MTEiOjAsInVwZ3Jh ZGVfcnVsZTNfYXQyMDE5MTEiOjAsInVwZ3JhZGVydWxlIjowfSwic2NvcmUiOnsibW9kZWxfdmVy IjoidmVyXzIwMTlfMTFfMjkiLCJxdWVyeV90aW1lIjoiLTEiLCJzY29yZTEiOi05OS4wLCJzY29y ZTIiOi05OS4wLCJzY29yZTMiOi05OS4wLCJzY29yZTQiOi05OS4wLCJzY29yZTUiOi05OS4wLCJz Y29yZTYiOi05OS4wLCJzY29yZTciOi05OS4wLCJzY29yZTgiOi05OS4wfX0sImhvc3RuYW1lIjoi YjYzMjEwOTlhNDMxIiwicXVlcnlUaW1lIjoiMjAyMC0wMi0xMCAxMjozNTo1NC45NzYzMTYiLCJy ZXNwX2NvZGUiOiIxIiwicmVzcF9kZXNjIjoiXHU2N2U1XHU4YmUyXHU2MjEwXHU1MjlmIiwic2l0 ZV92ZXIiOiJ6aG9uZ2JhbmdfMjAyMF8wMV8xMyJ9LCJtZXNzYWdlIjoic3VjY2VzcyJ9Cg==
发现base64内容都有空格,替换掉空格之后就好了
String res = "eyJjb2RlIjoxMDAwMDAsImRhdGEiOnsiZGF0YSI6eyJydWxlIjp7ImRhZXhpbmtlcnVsZSI6IjAu MTAwMTAwMCIsImxhb2tlcnVsZSI6IjAuMDAiLCJyZWplY3RfcnVsZTFfYXQyMDE5MTEiOjAsInVw Z3JhZGVfcnVsZTFfYXQyMDE5MTEiOjAsInVwZ3JhZGVfcnVsZTJfYXQyMDE5MTEiOjAsInVwZ3Jh ZGVfcnVsZTNfYXQyMDE5MTEiOjAsInVwZ3JhZGVydWxlIjowfSwic2NvcmUiOnsibW9kZWxfdmVy IjoidmVyXzIwMTlfMTFfMjkiLCJxdWVyeV90aW1lIjoiLTEiLCJzY29yZTEiOi05OS4wLCJzY29y ZTIiOi05OS4wLCJzY29yZTMiOi05OS4wLCJzY29yZTQiOi05OS4wLCJzY29yZTUiOi05OS4wLCJz Y29yZTYiOi05OS4wLCJzY29yZTciOi05OS4wLCJzY29yZTgiOi05OS4wfX0sImhvc3RuYW1lIjoi YjYzMjEwOTlhNDMxIiwicXVlcnlUaW1lIjoiMjAyMC0wMi0xMCAxMjozNTo1NC45NzYzMTYiLCJy ZXNwX2NvZGUiOiIxIiwicmVzcF9kZXNjIjoiXHU2N2U1XHU4YmUyXHU2MjEwXHU1MjlmIiwic2l0 ZV92ZXIiOiJ6aG9uZ2JhbmdfMjAyMF8wMV8xMyJ9LCJtZXNzYWdlIjoic3VjY2VzcyJ9Cg=="; System.out.println(new String(Base64.getDecoder().decode(res.replaceAll(" ", "")), "UTF-8"));
正确的做法是编码的时候就要把空格去除。
以上是关于Base64 解码decode遇到IllegalArgumentException: Illegal base64 character 20的主要内容,如果未能解决你的问题,请参考以下文章
Java进行Base64的编码(Encode)与解码(Decode)
Java如何进行Base64的编码(Encode)与解码(Decode)?
Django 2、python 3.4 无法解码 urlsafe_base64_decode(uidb64)