努力当误差en code /德code字符串为Base64 [英] Error when trying to encode/decode String to Base64

查看:672
本文介绍了努力当误差en code /德code字符串为Base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的字节数组Base64编码到stirng,而不是到另一个字节数组。但是,当我去code回来,我得到异常。这里是code

我想带code字节数组使用Base64编码的字符串。当我连接code,似乎工作,但是当我去code将其抛出异常。我在做什么错了?

 进口org.springframework.security.crypto codec.Base64。字节[]字节=新的字节[] {1,2,3,4,5,6,7,8,9};
字符串stringToStore = Base64.en code(字节)的ToString();
字节[] = restoredBytes Base64.de code(stringToStore.getBytes());

下面是我得到异常:

  org.springframework.security.crypto codec.InvalidBase64CharacterException:坏的Base64输入字符小数91阵列位置0
在org.springframework.security.crypto。codec.Base64.de code(Base64.java:625)
在org.springframework.security.crypto。codec.Base64.de code(Base64.java:246)


解决方案

您可以试试...

 字节[]字节=新的字节[] {1,2,3,4,5,6,7,8,9};
字符串stringToStore =新的String(Base64.en code(字节));
字节[] = restoredBytes Base64.de code(stringToStore.getBytes());

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

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

Here is the exception I'm getting:

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)

解决方案

Could you try...

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

这篇关于努力当误差en code /德code字符串为Base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆