Java Base64编码器未编码UTF-8
所以我有一个程序应该编码UTF-8 Base64,但不是。我的编码器代码如下:Java Base64编码器未编码UTF-8,java,base64,apache-commons,Java,Base64,Apache Commons,所以我有一个程序应该编码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).getByt
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功能)解码时:我得到:
输入(由我的代码生成):
网站的预期输出:
‰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`�
°PNG
IHDRÿa text author minecraftinfo.comßf'K²IDAT8Ëcÿÿÿ@您Base64.encodeBase64
将字节[]
作为输入。您应该将文件.readAllBytes(path.get(path))
的结果直接传递给Base64.encodeBase64
�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`�