【问题标题】:How to persist image in mysql database using Spring MVC and JPA如何使用 Spring MVC 和 JPA 将图像持久保存在 mysql 数据库中
【发布时间】:2015-03-05 02:19:13
【问题描述】:

这是我的 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>

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

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

【问题讨论】:

  • “Image”类及其 JPA 映射在哪里?
  • 我认为您必须将图像又名文件作为MultiPartFile 获取,并从该对象获取信息和图像本身。看这里just-thor.com/2014/03/01/…
  • 我对我的问题进行了编辑。您现在可以查看我的模型类和 persistence.xml。
  • 它是否正确持久化?它是否正确地从 JPA 中检索到?
  • 两个问题都是。我可以看到数据已保存在数据库中,但我认为它无法完全确定数据类型。 (数据库中的 Blob)

标签: mysql spring jsp spring-mvc jpa


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    相关资源
    最近更新 更多