I wanted to display image on jsp page but after running page I am getting this type of output on jsp page but not seen the actual image.
code from listFile.jsp page
<%@page import="org.hibernate.internal.build.AllowSysOut"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.studyeasy.hibernate.entity.Files"%>
<%@ page import="java.util.List"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Listing Images</title>
</head>
<body>
<h1>Listing Images!</h1>
<%
String path = (String) request.getAttribute("path");
List<Files> files = (List<Files>) request.getAttribute("files");
for (Files file : files) {
out.print("<br/><img src=\""+path+file.getFileName()+"\">");
}
%>
</body>
</html>
here is my listFiles() method responsible for getting list of image from database.
public List<Files> listFiles(){
Session session = factory.getCurrentSession();
session.beginTransaction();
List<Files> files = session.createQuery("from files").getResultList(); // can work only hibernate 5.2 or above
session.getTransaction().commit();
return files;
}
I am using ubuntu os and I am already set the image upload folder path to my local system directory which is
String path = "/home/user/image/";
once I upload image all the images are stored inside this path location and the file name are added on database as well following code responsible to add file on database.
public void fileUpload(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
try {
List<FileItem> images = upload.parseRequest(request);
for (FileItem image : images) {
String name = image.getName();
// System.out.println(name);
File file = new File(path + name);
if (!file.exists()) {
// uploading file into database
// checking file duplicate or not the storing to database.
new FilesDAO().addFileDetails(new Files(name));
// getting only file name
/*
* name = name.substring(name.lastIndexOf("//")+1);)
*/
// copying file to local system folder
image.write(file);
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listingImages(request, response);
}
implimentation of addFilesDeails() method
public void addFileDetails(Files file) {
Session session = factory.getCurrentSession();
session.beginTransaction();
session.save(file);
session.getTransaction().commit();
System.out.println(file.getFileName()+"Got Added!");
}
How do I correct this code to display a proper image on jsp page?