如何使用SpringMVC和JPA在mysql数据库中持久化图像
这是我的imageForm.jsp:如何使用SpringMVC和JPA在mysql数据库中持久化图像,mysql,spring,jsp,spring-mvc,jpa,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&g
%@ 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>
这是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页面会显示以下内容:配置文件图片:[B@1c6a19f
该条目被持久保存在数据库中,但在image属性下显示:[BLOB-39b]
如果我点击它,我会下载一个.bin文件。我不知道该如何处理这个问题,有人能帮我吗?最好的方法是使用FileOutputStream将图片保存在任何工作目录中,在db中,您可以保存唯一的图片名称或路径。您还可以从客户端接收字节[]在base64格式中。问题可能是您得到一个字节数组,表示为字符串,如asd561$%@!,然后使用base64.getDecoder.decodeyour_字符串。图像类及其JPA映射在哪里?我想您必须将图像aka文件作为MultiPartFile获取,并从该对象获取信息和图像本身。请参见我在中所做的编辑我的问题。你现在可以查看我的模型类和persistence.xml。它是否正确地持久化了?它是否正确地从JPA检索到了?这两个问题都是。我可以看到数据已持久化到数据库中,但我认为它无法完全确定数据库中的datatype.Blob
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;
}
}
<?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>