Error when trying to encode/decode String to Base64(尝试将字符串编码/解码为 Base64 时出错)
问题描述
我需要从字节数组进行 Base64 编码,而不是另一个字节数组.但是当我将它解码回来时,我得到了异常.这是代码
I need to do Base64 encoding from byte array to stirng as opposed to another byte array. But when I decode it back I get exception. Here is the code
我正在尝试使用 Base64 编码将字节数组编码为字符串.当我编码时,它似乎可以工作,但是当我解码时,它会引发异常.我做错了什么?
I'm trying to encode a byte array into a string using Base64 encoding. When I encode, it seems to work, but when I decode it throws an exception. What am I doing wrong?
import org.springframework.security.crypto.codec.Base64;
byte[] bytes = new byte[]{1,2,3,4,5,6,7,8,9};
String stringToStore = Base64.encode(bytes).toString();
byte[] restoredBytes = Base64.decode(stringToStore.getBytes());
这是我得到的例外:
org.springframework.security.crypto.codec.InvalidBase64CharacterException: Bad Base64 input character decimal 91 in array position 0
at org.springframework.security.crypto.codec.Base64.decode(Base64.java:625)
at org.springframework.security.crypto.codec.Base64.decode(Base64.java:246)
推荐答案
你能不能试试...
byte[] bytes = new byte[]{1,2,3,4,5,6,7,8,9};
String stringToStore = new String(Base64.encode(bytes));
byte[] restoredBytes = Base64.decode(stringToStore.getBytes());
这篇关于尝试将字符串编码/解码为 Base64 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:尝试将字符串编码/解码为 Base64 时出错
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
