如何使用Spring MVC和JPA在mysql数据库中保存图像

时间:2015-01-07 08:28:14

标签: 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>

这是我的控制者:

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>

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

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

1 个答案:

答案 0 :(得分:0)

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