java base64解码 怎么是乱码呢

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java base64解码 怎么是乱码呢相关的知识,希望对你有一定的参考价值。

BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(sTotalString);
out.print(new String(b));
我要解码的字符串是:IQAAAAQAAABEAEEAVABFAAoAAAAyADAAMQAzAC0AMQAwAC0AMAA1AAQAAABUAEkATQBFAAgAAAAwADkAOgA1ADIAOgA1ADgABwAAAE4ATwBfAFIASQBOAEcAAwAAADUAMwA5AAYAAABzAHQAYQB0AHUAcwABAAAAMAAGAAAAZABhAHQAYQA1ADQAAwAAADAALgAwAAYAAABkAGEAdABhADMAMwAJAAAAMwAuADIAMwAwADYAMQAzADUABgAAAGQAYQB0AGEANQA5AAMAAAAwAC4AMAAGAAAAZABhAHQAYQAzADQACAAAADIALgA5ADIAMQAwADAANwAGAAAAZABhAHQAYQAzADEACQAAADEALgA5ADAAMwA5ADMANQAyAAYAAABkAGEAdABhADUAOAAIAAAAMwAxADkAMwAzADIALgAwAAYAAABkAGEAdABhADMAMgAJAAAAMgAuADQAMwAyADcAMgA1ADcABQAAAGQAYQB0AGEAMQAJAAAAMgA0AC4ANwA2ADQAOQAwADIABQAAAGQAYQB0AGEANAADAAAAMAAuADAABgAAAGQAYQB0AGEAMgA4AAoAAAAwAC4AOQAwADQAMAA3ADkAOAA1AAYAAABkAGEAdABhADcANQAFAAAAOAAwADQALgAwAAUAAABkAGEAdABhADIACAAAADIAMwAuADgAOAAyADMAOAAFAAAAZABhAHQAYQAzAAwAAAAwAC4AMAAwADQAMwA0ADAAMgA3ADcANQAFAAAAZABhAHQAYQA4AAkAAAAxADEAMQAuADIANQA1ADcAOQAFAAAAZABhAHQAYQA5AAgAAAA3ADIALgAyADMANgA2ADkABQAAAGQAYQB0AGEANgAIAAAAOQA4AC4ANgA2ADgAOQA4AAUAAABkAGEAdABhADcACAAAADEANgA3AC4AMQAwADAANwAGAAAAZABhAHQAYQA2ADAAAwAAAE4AYQBOAAYAAABkAGEAdABhADIANQAJAAAAMAAuADQANgA1ADkAMgA4ADgABgAAAGQAYQB0AGEAMgA0AAoAAAAwAC4ANwAyADkANgAwADAANgA3AAYAAABkAGEAdABhADYAMQADAAAATgBhAE4ABgAAAGQAYQB0AGEAMgA3AAkAAAAxAC4ANgAxADMAMgA4ADEAMgAGAAAAZABhAHQAYQAyADYACQAAADAALgA5ADgAMwAwADcAMgA5AAYAAABkAGEAdABhADQANgAJAAAAMwA3ADgALgA4ADAAMwAxADMABgAAAGQAYQB0AGEANAA3AAkAAAAzADYANgAuADIANgA3ADgAMgAGAAAAZABhAHQAYQA0ADgACQAAADMANwA5AC4AMwA1ADcANQA0AAYAAABkAGEAdABhADQAOQAIAAAAMwA4ADQALgA5ADMAMQA2AAYAAABkAGEAdABhADQAMgADAAAAMAAuADAABgAAAGQAYQB0AGEANAAzAAMAAAAwAC4AMAA=

会乱码的原因是你的编码不一致导致的
php中的urlencode的编码是和系统编码一致的(比如windows默认gb2312,ubuntu默认utf-8)
所以首先需要确定你的系统编码,之后根据得到的系统编码在调用java的decode方法的时候,将这个编码传入(考虑到你的例子中有繁体字,所以,建议你使用utf-8编码),以下是我使用utf-8编码的例子(php环境是ubuntun下)

参考技术A 解码的字符集不对,指定下字符集转码
new String(Base64.encode(str.getBytes("GBK")));
字符集编码多换几个试试。打印下应该没问题
参考技术B public static String GetImageStr(String imgFilePath)
byte[] data = null;

// 读取图片字节数组
try
InputStream in = new FileInputStream(imgFilePath);
data = new byte[in.available()];
in.read(data);
in.close();
catch (IOException e)
e.printStackTrace();


// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
参考技术C 文字编码前后的字符集一样吗?

比如之前是用UTF-8,
现在输出是用默认编码?追问

这个编码后的字符我在线解码就能的出来,为什么在java里解码就是乱码。恩恩输出的我没设置编码。

追答

在线能出来,那么服务器端的编码是什么?

1,被base64编码前的字符编码A是什么?
也就是说一段通过某字符集编过码的二进制串①,进行了Base64编码。
然后产生了你说给的字符串
2,然后进行Base64解码,产生了另一段二进制串②。同二进制串①应该是一致的。
3,最后使用字符编码A再次解码成字符串。

你好好考虑下,这个字符编码A是什么,并且需要统一的。

本回答被提问者采纳
参考技术D

System.out.print(new String(b,"UTF-8"));

加密前是什么代码,解密后编码就写什么

追问

我写gb2312和utf-8还是乱码。。。大神们帮帮我啊

追答

看下加密的写法?

java base64解码 怎么是乱码呢

BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(sTotalString);
out.print(new String(b));
我要解码的字符串是:IQAAAAQAAABEAEEAVABFAAoAAAAyADAAMQAzAC0AMQAwAC0AMAA1AAQAAABUAEkATQBFAAgAAAAwADkAOgA1ADIAOgA1ADgABwAAAE4ATwBfAFIASQBOAEcAAwAAADUAMwA5AAYAAABzAHQAYQB0AHUAcwABAAAAMAAGAAAAZABhAHQAYQA1ADQAAwAAADAALgAwAAYAAABkAGEAdABhADMAMwAJAAAAMwAuADIAMwAwADYAMQAzADUABgAAAGQAYQB0AGEANQA5AAMAAAAwAC4AMAAGAAAAZABhAHQAYQAzADQACAAAADIALgA5ADIAMQAwADAANwAGAAAAZABhAHQAYQAzADEACQAAADEALgA5ADAAMwA5ADMANQAyAAYAAABkAGEAdABhADUAOAAIAAAAMwAxADkAMwAzADIALgAwAAYAAABkAGEAdABhADMAMgAJAAAAMgAuADQAMwAyADcAMgA1ADcABQAAAGQAYQB0AGEAMQAJAAAAMgA0AC4ANwA2ADQAOQAwADIABQAAAGQAYQB0AGEANAADAAAAMAAuADAABgAAAGQAYQB0AGEAMgA4AAoAAAAwAC4AOQAwADQAMAA3ADkAOAA1AAYAAABkAGEAdABhADcANQAFAAAAOAAwADQALgAwAAUAAABkAGEAdABhADIACAAAADIAMwAuADgAOAAyADMAOAAFAAAAZABhAHQAYQAzAAwAAAAwAC4AMAAwADQAMwA0ADAAMgA3ADcANQAFAAAAZABhAHQAYQA4AAkAAAAxADEAMQAuADIANQA1ADcAOQAFAAAAZABhAHQAYQA5AAgAAAA3ADIALgAyADMANgA2ADkABQAAAGQAYQB0AGEANgAIAAAAOQA4AC4ANgA2ADgAOQA4AAUAAABkAGEAdABhADcACAAAADEANgA3AC4AMQAwADAANwAGAAAAZABhAHQAYQA2ADAAAwAAAE4AYQBOAAYAAABkAGEAdABhADIANQAJAAAAMAAuADQANgA1ADkAMgA4ADgABgAAAGQAYQB0AGEAMgA0AAoAAAAwAC4ANwAyADkANgAwADAANgA3AAYAAABkAGEAdABhADYAMQADAAAATgBhAE4ABgAAAGQAYQB0AGEAMgA3AAkAAAAxAC4ANgAxADMAMgA4ADEAMgAGAAAAZABhAHQAYQAyADYACQAAADAALgA5ADgAMwAwADcAMgA5AAYAAABkAGEAdABhADQANgAJAAAAMwA3ADgALgA4ADAAMwAxADMABgAAAGQAYQB0AGEANAA3AAkAAAAzADYANgAuADIANgA3ADgAMgAGAAAAZABhAHQAYQA0ADgACQAAADMANwA5AC4AMwA1ADcANQA0AAYAAABkAGEAdABhADQAOQAIAAAAMwA4ADQALgA5ADMAMQA2AAYAAABkAGEAdABhADQAMgADAAAAMAAuADAABgAAAGQAYQB0AGEANAAzAAMAAAAwAC4AMAA=

会乱码的原因是你的编码不一致导致的
php中的urlencode的编码是和系统编码一致的(比如windows默认gb2312,ubuntu默认utf-8)
所以首先需要确定你的系统编码,之后根据得到的系统编码在调用java的decode方法的时候,将这个编码传入(考虑到你的例子中有繁体字,所以,建议你使用utf-8编码),以下是我使用utf-8编码的例子(php环境是ubuntun下)

参考技术A 解码的字符集不对,指定下字符集转码
new String(Base64.encode(str.getBytes("GBK")));
字符集编码多换几个试试。打印下应该没问题
参考技术B public static String GetImageStr(String imgFilePath)
byte[] data = null;

// 读取图片字节数组
try
InputStream in = new FileInputStream(imgFilePath);
data = new byte[in.available()];
in.read(data);
in.close();
catch (IOException e)
e.printStackTrace();


// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
return encoder.encode(data);
参考技术C 文字编码前后的字符集一样吗?

比如之前是用UTF-8,
现在输出是用默认编码?追问

这个编码后的字符我在线解码就能的出来,为什么在java里解码就是乱码。恩恩输出的我没设置编码。

追答

在线能出来,那么服务器端的编码是什么?

1,被base64编码前的字符编码A是什么?
也就是说一段通过某字符集编过码的二进制串①,进行了Base64编码。
然后产生了你说给的字符串
2,然后进行Base64解码,产生了另一段二进制串②。同二进制串①应该是一致的。
3,最后使用字符编码A再次解码成字符串。

你好好考虑下,这个字符编码A是什么,并且需要统一的。

本回答被提问者采纳
参考技术D

System.out.print(new String(b,"UTF-8"));

加密前是什么代码,解密后编码就写什么

追问

我写gb2312和utf-8还是乱码。。。大神们帮帮我啊

追答

看下加密的写法?

以上是关于java base64解码 怎么是乱码呢的主要内容,如果未能解决你的问题,请参考以下文章

BASE64解码之中文乱码问题

怎么解决js解码base64中文乱码问题

为啥我base64解码出来的字符串会带乱码

java Base64加密解密中文乱码处理

Java 中,对Oracle Clob中的图片Base64进行解码后用System.out.println() 输出为啥成乱码,求高手帮忙。

怎么让tableExport支持中文导出