[英]How to display an blob image received from database using Spring Boot
Good day dear community.亲爱的社区,美好的一天。 I got a problem when trying to display received image from MySQL via Base64.尝试通过 Base64 显示从 MySQL 接收到的图像时遇到问题。 Image uploaded and stored on DB without a problems.图像上传并存储在数据库中没有问题。
My model class:我的模型类:
@Entity
public class PostModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column (name = "title")
private String title;
@Column (name = "preview")
private String preview;
@Column (name = "content")
private String content;
@Column (name = "views")
private int views;
@Lob
@Column (name = "image")
private byte[] image;
//Getters and setters
Controller:控制器:
@GetMapping("/blog/{id}")
public String showContent(@PathVariable(value = "id") long id, Model model) throws
UnsupportedEncodingException {
if (!postRepository.existsById(id)) {
return "redirect:/post_not_exist";
}
Optional<PostModel> post = postRepository.findById(id);
ArrayList<PostModel> content = new ArrayList<>();
post.ifPresent(content::add);
model.addAttribute("post", content);
byte[] encodeBase64 = Base64.getEncoder().encode(post.get().getImage());
String base64Encoded = new String(encodeBase64, "UTF-8");
model.addAttribute("contentImage", base64Encoded );
return "post_content";
}
And HTML tag:和 HTML 标签:
<img src="data:image/jpg;base64,${contentImage}"/>
For result, I have this: The problem element结果,我有这个:问题元素
What I doing wrong?我做错了什么?
Good wishes.美好的愿望。
You need to add to the view with modelAndView.addObject("contentImage",base64Encoded );
您需要使用modelAndView.addObject("contentImage",base64Encoded );
添加到视图中modelAndView.addObject("contentImage",base64Encoded );
and also import ModelAndView
and change your method to ModelAndView
and instance the class ModelAndView
with ModelAndView modelAndView = new ModelAndView("view");
并导入ModelAndView
并将您的方法更改为ModelAndView
并使用ModelAndView modelAndView = new ModelAndView("view");
实例化类ModelAndView
ModelAndView modelAndView = new ModelAndView("view");
like this:像这样:
import org.springframework.web.servlet.ModelAndView;
@GetMapping("/blog/{id}")
public ModelAndView showContent(@PathVariable(value = "id") long id, Model model) throws
UnsupportedEncodingException {
if (!postRepository.existsById(id)) {
return "redirect:/post_not_exist";
}
Optional<PostModel> post = postRepository.findById(id);
ArrayList<PostModel> content = new ArrayList<>();
post.ifPresent(content::add);
model.addAttribute("post", content);
byte[] encodeBase64 = Base64.getEncoder().encode(post.get().getImage());
String base64Encoded = new String(encodeBase64, "UTF-8");
model.addAttribute("contentImage", base64Encoded );
ModelAndView modelAndView = new ModelAndView("view");
modelAndView.addObject("contentImage",base64Encoded );
return modelAndView;
}
With this, you can call the variables returned from `modelAndView and you can add more values if you want.有了这个,您可以调用从`modelAndView 返回的变量,如果需要,您可以添加更多值。
Here is a link that can help you with this topic with some examples: ModelAndView这是一个链接,可以通过一些示例帮助您解决此主题: ModelAndView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.