【发布时间】:2021-12-19 02:46:01
【问题描述】:
亲爱的社区,美好的一天。尝试通过 Base64 显示从 MySQL 接收到的图像时出现问题。图片上传并存储在数据库中没有问题。
我的模型课:
@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
控制器:
@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";
}
和HTML标签:
<img src="data:image/jpg;base64,${contentImage}"/>
对于结果,我有这个:The problem element
我做错了什么?
祝你好运。
【问题讨论】:
标签: java html mysql spring-mvc base64