gpt4 book ai didi

java - Base64 编码器未编码 UTF-8

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:21 24 4
gpt4 key购买 nike

所以我有一个程序应该编码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 格式并正确解码。那么我做错了什么?

最佳答案

Base64.encodeBase64byte[]作为输入。您应该将 Files.readAllBytes(Paths.get(path)) 的结果直接传递给 Base64.encodeBase64

关于java - Base64 编码器未编码 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25489131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com