Home > Software design >  Base64encoding and base64decoding in dart and Java
Base64encoding and base64decoding in dart and Java

Time:02-23

Is base64encode and decode in dart is same for Java. In my flutter app I want to upload an image. I convert that image to string by base64Encode(file. readAsBytesSync()). And then I passed it to backend. Backendcode is in JAVA. I want to decode this image file in java for save into a folder. How it is possible. Is I decode image gives same result.? I want to fetch it back also. Please help

CodePudding user response:

Putting all my comments as an answer here are 2 utility functions

public static BufferedImage decode(String base64Image)throws Exception 
{
  Base64.Decoder decoder=Base64.getDecoder();

  ByteArrayInputStream decoded=new ByteArrayInputStream(decoder.decode(base64Image));

  return ImageIO.read(decoded);
}

public static void writeImage(ByfferedImage img,File file)throws Exception 
{
 ImageIO.write(img,"png",file);
}
  • Related