Answer the question
In order to leave comments, you need to log in
How to display an image from a database in Spring?
Guys, I understand that it’s a stupid question and you can find examples on the Internet on how to do it, but basically I found Indians with incomprehensible code ... I have a blog database on localhost, I successfully upload all the information for the post there (name, date, text and picture ). But I can’t figure out how to display it from there. Maybe someone is not too lazy and will show at least part of the code or the idea itself ... Here I have a controller where I load information into the database
@PostMapping("addArticle")
public String postAddArticle(@AuthenticationPrincipal User user, Date timeArticle, @RequestParam String title, String author,
@RequestParam String anons, @RequestParam String text,
@RequestParam("file") MultipartFile file, Model model) {
Byte[] bArray = null;
author = user.getUsername();
Calendar calendar = Calendar.getInstance();
timeArticle = calendar.getTime();
try {
bArray = new Byte[file.getBytes().length];
int i = 0;
for(byte b : file.getBytes()){
bArray[i++] = b;
}
} catch (IOException e) {
e.printStackTrace();
}
Post post = new Post(title,timeArticle, author, anons, text, bArray );
post.setTitle(title);
post.setTimeArticle(timeArticle);
post.setAuthor(author);
post.setAnons(anons);
post.setText(text);
post.setImage(bArray);
postRepository.save(post);
return "redirect:/adminPage";
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
I have a blog database on localhost, I successfully load all the information for the post there (name, date, text and image)
postAddArticle(@AuthenticationPrincipal User user, Date timeArticle, @RequestParam String title, String author,
@RequestParam String anons, @RequestParam String text,
@RequestParam("file") MultipartFile file, Model model)
<img src="data:image/jpeg;base64,[тут_код_картинки_в_base64]">
byte[] encodeBase64 = Base64.encode(repository.getImage());
String base64Encoded = new String(encodeBase64, "UTF-8");
model.addObject("image", base64Encoded );
<img src="data:image/jpeg;base64,${image}" />
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question