Thursday, May 19, 2011

Retrieve Image From Database into Jsp

The Code Snipet shows how to retrieve image from an database. 
First retrieve related data to an ArrayList and get that arrayList use iterator to access elements following code shows how to retrieve and save image  to web folder



<%
ArrayList al=(ArrayList)request.getAttribute("AL");
Iterator it=al.iterator();
while(it.hasNext())
{
    LoginBean lbb=(LoginBean)it.next();
    if (lbb.getPicture()!=null)
{
         Someid = lbb.getid();
//System.out.println("********ID :" + admno);
InputStream sImage = lbb.getPicture();
        System.out.println("sImage==@@" + sImage.available());
        File f = new File(request.getRealPath("/")+"image" + Someid + ".jpg");
        System.out.println("-----"+request.getRealPath("/")+"image" + Someid + ".jpg");
        OutputStream out1 = new FileOutputStream(f);
        byte buf[] = new byte[1024];
        int len;
        while ((len = sImage.read(buf)) > 0)
{
            out1.write(buf, 0, len);
        }
        out1.close();
        sImage.close();
        System.out.println("\nFile is created.................");
}
%>






To show that image :


 <td colspan="2" class="columnname"> <img src ="<%=request.getContextPath()+"/image" + Someid + ".jpg"%>"  alt="" width="90" height="90"  align="right" border="3" /></td>

No comments:

Hide Query By Example of PanelCollection on page load

Hi All, If there is a table with filterable is true. If you want to display QueryByExample then surround this table with panelCollection....