【问题标题】:How to display an blob image received from database using Spring Boot如何使用 Spring Boot 显示从数据库接收到的 blob 图像
【发布时间】: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


    【解决方案1】:

    您需要使用modelAndView.addObject("contentImage",base64Encoded ); 添加到视图中,还需要导入ModelAndView 并将您的方法更改为ModelAndView 并使用ModelAndView modelAndView = new ModelAndView("view"); 实例化ModelAndView 类,如下所示:

    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;
    
    }
    

    这样,您可以调用从 `modelAndView 返回的变量,并且可以根据需要添加更多值。

    这是一个链接,可以通过一些示例帮助您了解该主题:ModelAndView

    【讨论】:

    • 对不起,但我的问题仍然没有解决...我将您的 anwser 标记为解决方案,我得到了 base64 图像并尝试在 HTML 页面中设置它:&lt;img th:src="@{'data:image/jpeg;base64,'+${contentImage}}"&gt;。但是它没有显示并返回:&lt;img src="data:image/jpeg;base64,cHJlc2lkZW50LmpwZw=="&gt; 我从其他程序员那里看到了很多类似的案例,这不起作用。请问,如果你知道这个,你能帮帮我吗?我想为你付出声望,但我还是做不到……
    • 你确定那是base64格式的正确图片吗?是因为我测试了该图像并且不起作用。你能把图片添加到评论中吗?
    • 查看这篇文章以将您的图像(以字节为单位)转换为 base64。 stackoverflow.com/questions/36492084/…
    • 是的!好的!你的建议对我有帮助! :) 我在上传图片到数据库时发现了一个问题,图片上传不正确,我的二进制代码有误。太感谢了!抱歉,我还是初学者……
    • 没问题,这对你来说是更多的经验,我希望你继续成长。
    猜你喜欢
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2014-02-21
    • 1970-01-01
    • 2013-01-23
    • 2017-08-29
    • 2019-04-19
    相关资源
    最近更新 更多