It may be a duplicate, but I ran into some problem to convert the image to Base64 to send it to Http Post . I tried this code, but it gave me the wrong encoded string.
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) {
Output: [B @ 677327b6
But I converted the same image in Base64 to many online encoders, and all of them gave the correct large Base64 string.
Edit: How is this a duplicate? The link, which is a duplicate of mine, does not give me the decision to convert the string that I wanted.
What am I missing here?
java image base64
setu basak
source share