So, i have a form that allows user to upload an image, which will be encoded with IOUtils.toByteArray and persisted to a database as a bytea. In a controller method i get this byte array and encode it to string:
@GetMapping("/{user_id}")
public String view(@PathVariable("user_id") Long user_id, Model model) {
User user = userService.getById(user_id);
model.addAttribute("user", user);
byte[] profilePictureBytes = user.getProfilePicture();
if (profilePictureBytes != null) {
String encodedImage = Base64.getEncoder().encodeToString(profilePictureBytes);
model.addAttribute("encodedImage", encodedImage);
}
return "user-page";
}
On a user-page html file i try to decode it like this:
<img th:attr="src=${'data:image/jpeg;base64,' + encodedImage}" alt="Profile Picture">
This solution works for smaller images but throws an exception when the encoded image exceeds 100 000 characters:
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "'data:image/jpeg;base64,' + encodedImage" (template: "user-page" - line 27, col 6)
org.springframework.expression.spel.SpelEvaluationException: EL1078E: Concatenated string is too long, exceeding the threshold of '100 000' characters
Is there a way to circumvent this limit or should i change the whole logic of the program instead? Thank you.
This one is tricky! TY, @Andrey(, again;) for exactly pointing the "issue". I'm not sure, whether it is worth to open a spring issue/request/enhancement for this.
But:
th:attrappend!! ;) (instead of Spring EL string concat;)Resulting template:
(Test) Controller used:
Generally
"These type of things" belong "into the caches of load balancers"! (..and not into the "memory" (
byte[], Base64...) of your "back ends" :)Advanced
It'd be nice to "attach" these (kind of) things (to html/http response) similar to https://www.thymeleaf.org/doc/articles/springmail.html, like:
MimeMessageaddInline)Another (Simple) Workaround
...serve the complete attribute value instead of the "base64 part".
Template then looks like:
(I also tried with template SpEL
th:attr="src=${#T(java.lang.String).format('data:image/jpeg;base64,%s', encodedImageAttr)}", but failed due to:)