简体   繁体   English

用Java解码字符串中的base64部分

[英]Decoding the base64 part in a string in Java

I have encoded into base64 using the Java class built in. But when I try to decode I am having some issues. 我已经使用内置的Java类对base64进行了编码。但是,当我尝试解码时,遇到了一些问题。 This code should explain what is happening: 此代码应解释发生了什么:

String secondhalf=text.substring(text.length() / 3,text.length() / 3* 2);
byte[] secondhalfByte = secondhalf.getBytes();
secondhalf = Base64.getDecoder().decode(secondhalfByte);

I am getting the error: 我收到错误消息:

cannot convert byte[] to String 无法将byte []转换为String

What do I do? 我该怎么办?

To get an encoded string or a decoded byte[] use the following: 要获取编码的字符串或解码的byte [],请使用以下命令:

String secondhalf=text.substring(text.length() / 3,text.length() / 3* 2);

String encoded = Base64.getEncoder().encodeToString(secondhalf); 
byte[] decoded = Base64.getDecoder().decode(encoded);

So you either want to encode your 'secondhalf' into a encoded string or then decode that into a byte[] 因此,您既可以将“ secondhalf”编码为编码的字符串,也可以将其解码为字节[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM