my problem is how to render the images in jsp from db, image upload was sucessfull but i am getting problem in rendering the images from DB i am using MySql Db
my Model class is
@Entity
@Table(name="photo")
public class Photo {
private int id;
private String name;
private MultipartFile file;
private byte[] imageContent;
private int imageId;
@Column(name="forId")
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="photoName")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Transient
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
@Lob
@Column(name="image",columnDefinition="blob")
public byte[] getImageContent() {
return imageContent;
}
public void setImageContent(byte[] imageContent) {
this.imageContent = imageContent;
}
}
LIst of Images jsp page (in this page only i have to display images from DB)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:out value="${message }"></c:out>
<img src='<c:url value="/image?imageId=${imageId }"/>'/>
</body>
</html>
controller is
@Controller
public class HomeController {
@Autowired
private PhotoService photoService;
public PhotoService getPhotoService() {
return photoService;
}
public void setPhotoService(PhotoService photoService) {
this.photoService = photoService;
}
public static Logger getLogger() {
return logger;
}
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(@ModelAttribute("photo")Photo photo,Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
return "home";
}
@RequestMapping(value="/uploadPhoto",method=RequestMethod.POST)
public String uploadImage(@ModelAttribute("photo")Photo photo,@RequestParam("file")MultipartFile multipartFile,Model model) {
byte[] imageContent=null;
try {
imageContent =photo.getFile().getBytes();
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println(imageContent);
photo.setImageContent(imageContent);
this.photoService.storePhoto(photo);
model.addAttribute("message", photo.getName());
model.addAttribute("imageId", photo.getImageId());
return"listOfImages";
}
@RequestMapping("/image")
public String getImages(@RequestParam("imageId")int imageId,HttpServletResponse response,Model model) {
byte[] image=null;
try {
image=this.photoService.getPhotoByPhotoId(imageId).getImageContent();
response.setContentType("image/jpg");
response.getOutputStream().write(image);
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return"listOfImages";
}
}
Dao implementation for this is
@Repository("photoDao")
public class PhotoDaoImpl implements PhotoDao {
@Autowired
private SessionFactory SessionFactory;
public SessionFactory getSessionFactory() {
return SessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
SessionFactory = sessionFactory;
}
/**
* This method is used to store the photo attributes to DB
*/
@Override
public Photo storePhoto(Photo photo) {
Session session =this.SessionFactory.getCurrentSession();
session.save(photo);
return photo;
}
@Override
public Photo getPhotoByPhotoId(int id) {
Session session =this.SessionFactory.getCurrentSession();
Photo photo =null;
photo = (Photo)session.get(Photo.class, id);
return photo;
}
}
Service implementation for this is
@Service("photoSertvice")
public class PhotoServiceImpl implements PhotoService {
@Autowired
private PhotoDao photoDao;
public PhotoDao getPhotoDao() {
return photoDao;
}
public void setPhotoDao(PhotoDao photoDao) {
this.photoDao = photoDao;
}
@Override
@Transactional
public Photo storePhoto(Photo photo) {
return this.photoDao.storePhoto(photo);
}
@Override
@Transactional
public Photo getPhotoByPhotoId(int id) {
return this.photoDao.getPhotoByPhotoId(id);
}
}