所以我有一个假设的程序来编码UTF-8 Base64,但事实并非如此。我的编码器代码如下:
public class Encoder {
public static void Encode() throws IOException{
byte[] bytes = Base64.encodeBase64(readFile("C:\\Users\\Dragon\\Desktop\\Binary\\Diamond.png", StandardCharsets.UTF_8).getBytes(StandardCharsets.UTF_8));
String binary = new String(bytes);
PrintWriter out = new PrintWriter("C:\\Users\\Dragon\\Desktop\\Binary\\Base64.txt");
out.println(binary);
out.close();
System.out.println("Your File has been saved at C:\\Users\\Dragon\\Desktop\\Binary\\Base64.txt");
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
当我用这个网站(具有UTF-8功能)解码时:http://www.base64decode.org/我得到: 输入(我的代码制作了这个):
77+9UE5HDQoaCgAAAA1JSERSAAAAEAAAABAIBgAAAB/vv73vv71hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23vv71mJ0sAAADvv71JREFUOO+/vWPvv73vv73vv70/AyUY77+9QAM877+9Bu+/ve+/vTHvv708XgNAGu+/vQHvv70hOA3vv71pXu+/ve+/vTkcX++/ve+/ve+/ve+/ve+/vSjvv71gNQDvv70Z77+9Ae+/vRkE77+977+9XgRj77+9De+/vWkEGQTvv70gA++/vV3vv71hAO+/ve+/vS/vv71Q77+977+977+9RhRX77+9NQBkIy4DYO+/ve+/vWkASO+/ve+/vdmNWA3vv73vv73vv70GY++/vQbvv70CDO+/vQEZ77+9DAVhEBvvv70B77+9QARpBu+/vQLvv70A77+9MDHvv71hADMA77+9R1wG77+9bCfvv73vv73vv73vv73vv73vv71077+977+9Mjbvv703KVPvv70b77+977+9AB0QYe+/vV4VTO+/vQAAAABJRU5E77+9QmDvv70=
网站的预期输出:
‰PNG
IHDR óÿa tEXtAuthor minecraftinfo.comßf'K ²IDAT8Ëcøÿÿ?%Á@<æÿ‘1º<^@к!8
€i^ôí9_üõù¿Áí½(†`5 ¦¤¦¼Þ^c¢
€ià ]a ºæž/P°ÂÔFWà5 d#.`®Ài H³ÎÙX
®Îc¼À¤ƒa«°@iÙÓ Ã01œa 3 äG\Àl'˜Œît¢“26Œ7)SœÉÅ aÍ^L‹ IEND®B`‚
来自网站的实际输出:
�PNG
IHDR��atEXtAuthorminecraftinfo.com�f'K�IDAT8�c���?%�@<���1�<^@��!8
�i^��9_�����(�`5�����^c�
�i� �]�a��/�P���FW�5d#.`��iH��ٍX
���c����a��@i���01�a3�G\�l'������t��26�7)S���a�^L�IEND�B`�
然而,当我使用这个网站而不是我的代码编码相同的东西时,它有正确的UTF-8格式和解码。那么我做错了什么?
答案 0 :(得分:1)
您Base64.encodeBase64
将byte[]
作为输入。您应该直接将Files.readAllBytes(Paths.get(path))
的结果传递给Base64.encodeBase64
。