Download images from Database in JSP

In this section, we have developed a web application to download images from database in JSP.

Download images from Database in JSP

Download images from Database in JSP

     

In this section, we have developed a web application to download images from database in JSP. Two files are used "image.jsp" and "imageDownload.jsp" in the code given below.

Brief description of the flow of application :

1). Create a webpage "imageDownload.jsp"  to display and download the image from database. All images will show as hyperlink image. 

2). Another  "image.jsp" is used to retrieve image.

 

Step:1 To create a "imageupload" table in Database 

CREATE TABLE `imageupload` (
`id` bigint(20) NOT NULL auto_increment,
`imagefile` blob NOT NULL,
PRIMARY KEY (`id`)
)

Step:2 To create a web page  "image.jsp"  

<%page import="java.sql.*,java.io.*,java.util.*" %> 
<%

String connectionURL = "jdbc:mysql://localhost:3306/userdetails";
if(request.getParameter("imgid")!=null && request.getParameter("imgid")!="")
{
  int id =  Integer.parseInt(request.getParameter("imgid"));

  String filename = "image"+id+".jpg";
  Connection con=null;
  try{  
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  con=DriverManager.getConnection(connectionURL,"root","root");  
  Statement st1=con.createStatement();
  String strQuery = "select imagefile from imageupload where id="+id;
  
  ResultSet rs1 = st1.executeQuery(strQuery);
  
  String imgLen="";
  if(rs1.next()){
  imgLen = rs1.getString(1);
 }  
  rs1 = st1.executeQuery(strQuery);
  if(rs1.next()){
  int len = imgLen.length();
  byte [] rb = new byte[len];
  InputStream readImg = rs1.getBinaryStream(1);
  int index=readImg.read(rb, 0, len);  
  st1.close();
  response.reset();
  response.setContentType("image/jpg");
  response.setHeader("Content-disposition","attachment; filename=" +filename);
  response.getOutputStream().write(rb,0,len)
  response.getOutputStream().flush();  
  }
  }
  catch (Exception e){
  e.printStackTrace();
  }
}
%>

 Step:3 To create a "imageDownload.jsp" 

<%page import="java.sql.*,java.io.*,java.util.*" %> 
<HTML>
 <HEAD>
  <TITLE>Download Images</TITLE>
  </HEAD>

 <BODY>
 <br><br>
  <table align="center" border=width="200px">
 <tr>
  <td colspan=align="center"><b>Download Images</b></td>
  </tr>
  <tr><td colspan=2>&nbsp;</td></tr>
  <%

  String connectionURL = "jdbc:mysql://localhost:3306/userdetails";
  Connection con=null;
  try{  
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  con=DriverManager.getConnection(connectionURL,"root","root");
  Statement stmt=con.createStatement();
  String strQuery = "select id from imageupload";
  ResultSet rs = stmt.executeQuery(strQuery);
  int sno=0;
  while(rs.next())
  {
  sno++;
  %>
  <tr style="background-color:#efefef;">
  <td><b><%=sno%></b></td>
  <td align="center">
<a href=
"image.jsp?imgid=<%=rs.getInt(1)%>">
<img src=
"image.jsp?imgid=<%=rs.getInt(1)%>" width="50" height="50">
</a></td>

  </tr>
  <%
  }
  rs.close();
  con.close();
  stmt.close();
  }
  catch(Exception e)
  {
  e.getMessage();
  }
  %>
 </table>
  </BODY>
</HTML>

Output:

Display images :

Download the image :

Download the application