参见英文答案 > Encoding as Base64 in Java 16个
它可能是重复但我面临一些问题,将图像转换为Base64发送它为Http Post.我试过这段代码,但它给了我错误的编码字符串.
public static void main(String[] args) {
File f = new File("C:/Users/SETU BASAK/Desktop/a.jpg");
String encodstring = encodeFileToBase64Binary(f);
System.out.println(encodstring);
}
private static String encodeFileToBase64Binary(File file){
String encodedfile = null;
try {
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
encodedfile = Base64.encodeBase64(bytes).toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encodedfile;
}
输出:[B @ 677327b6
但我在许多在线编码器中将同一图像转换为Base64,并且它们都给出了正确的大Base64字符串.
编辑:怎么重复?我的副本链接并没有给我转换字符串我想要的解决方案.
我在这里失踪了什么?