Java Base64 编码:非法的 base64 字符 3c

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46240495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:17:44  来源:igfitidea点击:

Base64 Encoding: Illegal base64 character 3c

javaencodingutf-8base64decoding

提问by VMA92

I am trying to decode data in an xml format into bytes base64and I am having an issues. My method is in java which takes a Stringdata and converts it into byteslike as bellow.

我正在尝试将 xml 格式的数据解码为字节base64,但遇到了问题。我的方法是在 java 中,它接受一个String数据并将其转换bytes为如下所示。

String data = "......"; //string of data in xml format
byte[] dataBytes = Base64.getDecoder().decode(data);

Which failed and gave the exception like bellow.

哪个失败并给出了如下异常。

java.lang.IllegalArgumentException: Illegal base64 character 3c
    at java.util.Base64$Decoder.decode0(Base64.java:714)
    at java.util.Base64$Decoder.decode(Base64.java:526)
    at java.util.Base64$Decoder.decode(Base64.java:549)
    at XmlReader.main(XmlReader.java:61)

Is the xml format not compatible with base64?

xml 格式不兼容base64吗?

采纳答案by VMA92

Thanks to @luk2302 I was able to resolve the issue. Before decoding the string, I need to first encode it to Base64

感谢@luk2302,我能够解决这个问题。在解码字符串之前,我需要先将其编码为 Base64

    byte[] dataBytes = Base64.getEncoder().encode(data.getBytes());
    dataBytes = Base64.getDecoder().decode(dataBytes);

回答by vanje

You should first get the bytes out of the string (in some character encoding).

您应该首先从字符串中取出字节(在某些字符编码中)。

For these bytes you use the encoder to create the Base64 representation for that bytes.

对于这些字节,您可以使用编码器为该字节创建 Base64 表示。

This Base64 string can then be decoded back to bytes and with the same encoding you convert these bytes to a string.

然后可以将此 Base64 字符串解码回字节,并使用相同的编码将这些字节转换为字符串。

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Example {

  public static void main(String[] args) {
    final String xml = "<root-node><sub-node/></root-node>";
    final byte[] xmlBytes = xml.getBytes(StandardCharsets.UTF_8);
    final String xmlBase64 = Base64.getEncoder().encodeToString(xmlBytes);
    System.out.println(xml);
    System.out.println(xmlBase64);

    final byte[] xmlBytesDecoded = Base64.getDecoder().decode(xmlBase64);
    final String xmlDecoded = new String(xmlBytesDecoded, StandardCharsets.UTF_8);
    System.out.println(xmlDecoded);
  }

}

Output is:

输出是:

<root-node><sub-node/></root-node>
PHJvb3Qtbm9kZT48c3ViLW5vZGUvPjwvcm9vdC1ub2RlPg==
<root-node><sub-node/></root-node>

回答by Moddasir

Just use this method

就用这个方法

getMimeDecoder()

getMimeDecoder()

String data = "......";
byte[] dataBytes =  Base64.getMimeDecoder().decode(data);

回答by André Pacheco

I got this same error and problem was that the string was starting with data:image/png;base64, ...

我遇到了同样的错误,问题是字符串以 data:image/png;base64, ...

The solution was:

解决方案是:

byte[] imgBytes = Base64.getMimeDecoder().decode(imgBase64.split(",")[1]);