Download images from Database in JSP

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


 
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Download images from Database in JSP

Posted on: June 15, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Download images from Database in JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
santosh
September 9, 2011
Error code

Download Images 1 2 3 4 5 no image here
Madhavi
February 26, 2013
Web Application

I need to know how to download the images to sql database from mobile?
B.Rajalingam
November 26, 2011
sample images

good job
saurav
January 20, 2012
image

thnx for giving such a good concept i reaaly appreciate you...thnx once again
Amarnath Dutta Roy
September 16, 2012
jsp

this website is very good,helpful and user friendly........ thank you........
SABARISH JAGANATHAN
April 9, 2013
formal

i have used this code but only id is displayed no image how to run ?

Ads

 

Ads