Как отобразить несколько изображений больших двоичных объектов из базы данных MySQL в JSP и сервлетов на основе условие WHERE
Я хочу отобразить несколько изображений blob из базы данных на основе идентификатора электронной почты но я могу получить только одно изображение и другое изображение связано с одним и тем же идентификатором электронной почты не отображается не отображается мне нужна ваша помощь для отображения нескольких изображений из базы данных
база данных.
Что я уже пробовал:
package controller; import java.io.*; import java.sql.SQLException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import Databse.AppDao; import com.mysql.jdbc.ResultSet; /** * Servlet implementation class Image */ @WebServlet("/Image") public class Image extends HttpServlet { private static final long serialVersionUID = 1 L; /** * @see HttpServlet#HttpServlet() */ public Image() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("image/jpg"); OutputStream img; java.sql.Blob image = null; byte[] imgData = null; HttpSession session = request.getSession(); // String email =(String)session.getAttribute("email"); String email = "tanumanu19971@gmail.com"; String sql = "select photo from photos where email=?"; AppDao obj = new AppDao(); java.sql.ResultSet rs = AppDao.getimage(sql, email); OutputStream o; try { while (rs.next()) { String imgLen = ""; imgLen = rs.getString(1); System.out.println(imgLen.length()); int len = imgLen.length(); byte[] rb = new byte[len]; InputStream readImg = rs.getBinaryStream(1); int index = readImg.read(rb, 0, len); System.out.println("index" + index); response.setContentType("image/jpg"); response.getOutputStream().write(rb, 0, len); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { while (rs.next()) { String imgLen1 = ""; imgLen1 = rs.getString(1); System.out.println(imgLen1.length()); int len1 = imgLen1.length(); byte[] rb = new byte[len1]; InputStream readImg = rs.getBinaryStream(1); int index = readImg.read(rb, 0, len1); System.out.println("index" + index); response.setContentType("image/jpg"); response.getOutputStream().write(rb, 0, len1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } AppDao class package Databse; import java.io.InputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletInputStream; import com.oreilly.servlet.multipart.Part; public class AppDao { public static String username = "root"; public static String password = "root"; public static String url = "jdbc:mysql://localhost:3306/wedding"; public static String driver = "com.mysql.jdbc.Driver"; public static Connection con = null; public static PreparedStatement ps = null; public static ResultSet rs = null; public static ResultSet rs1 = null; public static ResultSet ds = null; static { try { Class.forName(driver); con = DriverManager.getConnection(url, username, password); System.out.println("Connection successfull"); } catch (Exception e) { e.printStackTrace(); } } public static ResultSet getimage(String sql, String email) { try { ps = con.prepareStatement(sql); ps.setString(1, email); rs = ps.executeQuery(); } catch (Exception e) { } return rs; } Database wedding Create DATABASE wedding; use wedding; create table `photos` ( `id` int(10), `photo` blob, `security` varchar(105), `email` varchar(120) ); insert into `photos` (`id`, `photo`, `security`, `email`) values('1', 'ÿØÿà', 'tanumanu19971123', 'tanumanu19971@gmail.com'); insert into `photos` (`id`, `photo`, `security`, `email`) values('2', 'ÿØÿà', 'tanumanu19971123', 'tanumanu19971@gmail.com'); insert into `photos` (`id`, `photo`, `security`, `email`) values('3', 'ÿØÿà', 'tanumanu19971123', 'tanumanu19971@gmail.com'); Explanation I want to get multiple blob images from MySQl database from email id but after running this code only single image display but two other images which are associate with tanumanu19971 @gmail.com are not displaying i have used while loop and repeat the image content twice but could not get any result .I know that this question is asked before but trust me i have tried all post for me i am dealing with this situation from many days so please do not ignore my post or put it in hold and allow your community to help me out any modification in this code will be really helpful so that i can able to get multiple blob images from database based on email id