Mail attachment from local storage files

Hi there.

I have files stored on local storage, (uploaded using the FileStorage api) that i would like to send by email upon customer click on a button in a form.
I got up to the moment when i try to get the file bytes in an array so that i can attach it to the emailbuilder.
How would you get those file bytes in the simplest and cleanest way?
Actually, i would have to resort to lots of code and i wonder if there is something nice around that i could use.

Any hint is very appreciated, thanks.

Have a nice day.

Hi Frederic,

Provided that Customer entity has an image field which is a reference to a file in file storage, the following code loads the file into a byte array:

@Autowired
private FileStorage fileStorage;

public byte[] getImageBytes(Customer customer) {
    FileRef fileRef = customer.getImage();
    InputStream inputStream = fileStorage.openStream(fileRef);
    try {
        return IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        throw new RuntimeException("Error reading file storage content to byte array", e);
    }
}

But make sure the files your are loading this way are not too big to not overload the app memory.