Thursday, 31 March 2016

How to download images from database in JSP ?


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 url = "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(url, "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=0 width="200px">
   <tr>
    <td colspan=2 align="center"><b>Download Images</b></td>
  </tr>
  <tr><td colspan=2>&nbsp;</td></tr>
  <%
  String url = "jdbc:mysql://localhost:3306/userdetails";
  Connection conn = null;
  try {     
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    conn = DriverManager.getConnection(url, "root", "root");
    Statement stmt = conn.createStatement();
    String strQuery = "select id from imageupload";
   
    ResultSet rs = stmt.executeQuery(strQuery);
    int sno = 0;
    while(rs.next())
    {
      sno++;
%>
      <tr style="background-color:#efefef;" 
        mce_style="background-color:#efefef;" 
        mce_style="background-color:#efefef;">
      <td><b><%=sno%></b></td>
       <td align="center">
           <a href="image.jsp?imgid=<%=rs.getInt(1)%>
              mce_href="image.jsp?imgid=<%=rs.getInt(1)%>
              mce_href="image.jsp?imgid=<%=rs.getInt(1)%>">
           <img src="image.jsp?imgid=<%=rs.getInt(1)%>
              mce_src="image.jsp?imgid=<%=rs.getInt(1)%>
              mce_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>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.