mysql - 如何使用 Spring MVC 和 JPA 在 mysql 数据库中持久化图像

标签 mysql spring jsp spring-mvc jpa

这是我的 imageForm.jsp:

%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>
<head>
    <title></title>
</head>
<body>
<h2>Image Form</h2>
<form:form method="POST" action="/showImage">
  Picture: <input type="file" name="image">
  <br />
  <input type="submit" value="Submit" />
</form:form>
</body>
</html>

这是 showImage.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<h2>Show Image</h2>

<p>Profile Picture : ${image.image}</p>
</body>
</html>

这是我的 Controller :

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

    @RequestMapping(value = "/imageForm")
    public ModelAndView showImageForm(Model model) {

        return new ModelAndView("imageForm", "command", new Image());
    }

    @Transactional
    @RequestMapping(value = "/showImage")
    public ModelAndView showResult(@ModelAttribute("")Image image, ModelAndView model) {

        model.setViewName("showImage");
        System.out.println("Transaction");
        em.persist(image);
        System.out.println("persisted");
        model.addObject("image", image);
        return model;
    }
}

这是 Image.java 模型类:

package com.springapp.mvc;

import javax.persistence.*;


@Entity
public class Image {
    @Id
    private int imageID;
    private byte[] image;

    public int getImageID() {
        return imageID;
    }

    public void setImageID(int imageID) {
        this.imageID = imageID;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }


}

以及 persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

<persistence-unit name="NewPersistenceUnit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.springapp.mvc.Image</class>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/advocatoree"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>
</persistence-unit>

</persistence>

我可以从我的 PC 中选择一张图片,当我按下提交按钮时,showImage.jsp 页面会显示如下:Profile Picture : [B@1c6a19f

该条目保存在数据库中,但在图像属性下显示:[BLOB - 39 B] 如果我点击它,我会下载一个 .bin 文件。我不知道应该如何解决这个问题,有人可以帮助我吗?

最佳答案

最好的方法是使用 FileOutputStream 将图片保存在任何工作目录中,在数据库中,您可以保存唯一的图片名称或路径。您还应从客户端收到 base64 格式 的 byte[]。问题可能是您得到一个表示为字符串的字节数组(如“asd561$%@!”),然后使用 Base64.getDecoder().decode(your_string)。

关于mysql - 如何使用 Spring MVC 和 JPA 在 mysql 数据库中持久化图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815205/

相关文章:

javascript - 单击提交按钮时需要重置填充数据库值的文本框

mysql - SQL 根据销售数量获取每个用户订购最多的前 3 个产品

Java + Spring + Hibernate : "order by" slow on production

Spring @Transactional 和回滚不起作用,Spring 集成测试

java - 引用从不同目录中的 jsp 打包的 servlet

java - 如何清理 HTML 代码以防止 Java 或 JSP 中的 XSS 攻击?

jsp - JSF 中的响应对象

mysql - 合并两个 SELECT 查询

Mysql 找到共享帖子的根父级

Spring 数据: relationships between 2 different data sources