2

I'm making a client-server application and I would like to send images using a byte array. I'm using this code to convert the image into a byte array and then to a String:

// converting the image into bytes
BufferedImage bufferedImage = ImageIO.read(new File(filePath));
WritableRaster raster = bufferedImage.getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
byte[] bytes = data.getData();
System.out.println("Size of array is " + bytes.length);
String base64String = Base64.encode(bytes);
System.out.println("size of base64String to send is " + base64String.length());

I'm getting this output:

Size of array is 743904

size of base64String to send is 991872

size of the image that i want to send is 42.4KB

I'm using this code:

byte[] bytearray = Base64.decode(newMessage.getMessageToAdd());
System.out.println("Size of array is " + bytearray.length);
OutputStream outimg = null;
try {
    outimg = new BufferedOutputStream(new FileOutputStream(pathToSave));
    outimg.writewrite(bytearray);
} finally {
    if (outimg != null) {
        outimg.close();
    }
}

I get this output:

size of array base64String recived is 991872
Size of array is 743904
and the size of the image created is 726KB

So from the output I understand that it recived and sent the image without getting a error but for some reason it fails at creating the image.

Community
  • 1
  • 1

0 Answers0