I am trying to convert a image to byte[] which will be sent to a service, will determine the file size if it is allowable or not. But I am getting different size in of actual image file and byte[]. My test code:
File file=new File("path/photo.jpg"); //around 4.5 MB
img = ImageIO.read(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", bos );
byte [] data = bos.toByteArray();
System.out.println("fileSize:" file.length());
System.out.println("byteSize:" data.length);
output
fileSize:4544732 //equals to actual image size
byteSize:1468863
My target is: I need to measure the actual filesize from the byte[] of that image.
CodePudding user response:
ImageIO is not needed for your use-case.
Try this instead.
final byte[] content = Files.readAllBytes(file.toPath());