를 사용하여 MySQL 데이터베이스에 이미지를 유지하는 방법이 내 imageForm.jsp입니다 :는 스프링 MVC와 JPA
%@ 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;
}
}
63,210 그리고 persistence.xml 파일 : Profile Picture : [[email protected]
: 내가 제출 버튼을 누르면 내 PC에서 이미지를 선택할 수 있습니다
<?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 페이지 나에게이 문제를 보여줍니다 항목은 데이터베이스에 유지되지만 이미지 속성 아래에 다음과 같이 표시됩니다. [BLOB - 39 B]
클릭하면 .bin 파일이 다운로드됩니다. 나는이 문제에 어떻게 접근해야하는지, 누군가 나를 도울 수 있습니까?
'이미지'클래스와 JPA 매핑은 어디에 있습니까? –
나는 이미지를'MultiPartFile'이라는 파일로 가져와야하고 그 객체로부터 정보와 이미지 자체를 가져와야한다고 생각한다. 여기에서보십시오 http://www.just-thor.com/2014/03/01/spring-mvc-and-jpa-upload-and-retrieve-photo-to-postgres/ – sfat
나는 나의 질문을 편집했다. 이제 내 모델 클래스와 persistence.xml을 확인할 수 있습니다. –