How to view an image stored on database with JSP?

Ruggero Ruggeri

The following is a code snippet for viewing an image stored in an Interbase database:

<%@ page language="java" import="java.sql.*,java.util.*"%>
<%
	String image_id = (String) request.getParameter("ID");
	if (image_id != null){
          try 
          {
            Class.forName("interbase.interclient.Driver");
  	    Connection con = DriverManager.getConnection("jdbc:interbase://localhost/D:/InterBase/examples/Database/employee.gdb","java","java");
   	    Statement stmt = con.createStatement();
	    ResultSet rs = stmt.executeQuery("SELECT * FROM IMMAGINE WHERE IMMAGINE_ID = " + image_id);
	    if (rs.next())
            {
		String dim_image = rs.getString("IMMAGINE_DIMENSIONE");
		byte [] blocco = rs.getBytes("IMMAGINE_IMMAGINE");
		response.setContentType("image/jpeg");
		ServletOutputStream op = response.getOutputStream();
		for(int i=0;i<Integer.parseInt(dim_image);i++)
		{
			op.write(blocco[i]);
		}
  	   } 
	   rs.close();
	   stmt.close();
	   con.close();
	 } catch(Exception e) {
		out.println("An error occurs : " + e.toString());				
         }			
	}
%>
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact