In a servlet I had to read an image file from disk, encode it to Base64 and then send back to the client. Because I only found samples for iOS, Python and some other kinds (principally all do it the same way) I thought I post my code here to help others 😉
CAUTION!
This should only work for files up to 2GB: setting capacity of ByteBuffer only takes an int.
Why so complicated and why read the complete image into memory?
A much simpler solution: