Issue with Base64 encoding/decoding: decoded string is '?'

偶尔善良 提交于 2019-12-14 01:53:45

问题


I am trying to read an image and use Base64 encoding to convert it into byte array and then to string to send it over network. The problem is that when I try to decode the Base64 encoded string, I am getting incorrect data.

For eg. I am facing issue with below special character.

I am using following code for encoding:

byte[] b = Base64.encodeBase64(IOUtils.toByteArray(loInputStream));
String ab = new String(b);

IOUtils is org.apache.commons.io.IOUtils.

and loInput

Code for decoding:

byte[] c = Base64.decodeBase64(ab.getBytes());
String ca = new String(c);
System.out.println(ca);

It prints ? for decoded String.

Can anyone please let me know the issue.


回答1:


As I've said elsewhere, in Java, String is for text, and byte[] is for binary data.

String ≠ byte[]

Text ≠ Binary Data

An image is binary data. Base64 is an encoding which allows transmission of binary data over US_ASCII compatible text channels (there is a similar encoding for supersets of ASCII text: Quoted Printable).

So, it goes like:

Image (binary data) → Image (text, Base64 encoded binary data) → Image (binary data)

where you would use String encodeBase64String(byte[]) to encode, and byte[] decode(String) to decode. These are the only sane API's for Base64, byte[] encodeBase64(byte[]) is misleading, the result is US_ASCII-compatible text (so, a String, not byte[]).

Now, text has a charset and an encoding, String uses a fixed Unicode/UTF-16 charset/encoding combination internally, and you have to specify a charset/encoding when converting something from/to a String, either explicitly, or implicitly, using the platform's default encoding (which is what PrintStream.println() does). Base64 text is pure US_ASCII, so you need to use that, or a superset of US_ASCII. org.apache.commons.codec.binary.Base64 uses UTF8, which is a superset of US_ASCII, so all is well. (OTOH, the internal java.util.prefs.Base64 uses the platform's default encoding, so I guess it would break if you start your JVM with, say, an UTF-16 encoding).

Back on topic: you've tried to print the decoded image (binary data) as text, which obviously hasn't worked. PrintStream has write() methods that can write binary data, so you could use those, and you would get the same garbage as if you wrote the original image. It would be much better to use a FileOutputStream, and compare the resulting file with the original image file.




回答2:


If your input is an image, it makes sense to encode it as base64 - base64 is text, and can be represented by a String.

Decoding it again though, you get the original image. An image is usually a binary format; it does not make sense to try to convert that to a string - it is not text.

That is, the last 2 lines:

   String ca = new String(c);
   System.out.println(ca);

Simply does not make sense to do.

If you want to check that the decoding produces the same output as the original input, do e.g.

  System.out.println("Original and decoded are the same: " + Arrays.equals(b,c));

(Or save the byte array to a file and view the image in an image viewer)



来源:https://stackoverflow.com/questions/6484369/issue-with-base64-encoding-decoding-decoded-string-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!