Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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

                         

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

                         

» View all related tutorials
Related Tags: c exception error jsp io count page using invoke js handle if to ci counter e directive can pe ce

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.