Link to home
Start Free TrialLog in
Avatar of widiyantosg
widiyantosg

asked on

Displaying blob on jsp

Hi

I am trying to display my image that is saved inside mysql in blob format in jsp. but I can not display the picture.

in viewInventoryimage.jsp:
<%@ page import="db.*,java.sql.*" %>
<%
String id = request.getParameter("id");
out.println("id:"+id);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#F0F0F0">
<form action="form1" method="post" enctype="multipart/form-data">
<table width="45" height="44" border="1">
  <tr>
    <td width="36"><img src="inventory_action1.jsp?action=viewImage&id=<%=id%>"></td>
  </tr>
</table>
</form>
</body>
</html>

in inventory_action.jsp:
else if(action.equals("viewImage"))
{
sql = "SELECT image_path FROM inventory WHERE id = '"+id+"'";
rs = stmt.executeQuery(sql);
response.setContentType("image/jpeg") ;
if(rs.next())
{
byte[] bytearray = new byte[4096];
int size=0;
InputStream sImage;
sImage = rs.getBinaryStream(4);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=getimage.jpeg");
while((size=sImage.read(bytearray))!= -1 )
{
response.getOutputStream().write(bytearray,0,size);
}
response.flushBuffer();
sImage.close();
}
out.flush();
}
stmt.close();

can anybody tell me wat is the error? Thank you
SOLUTION
Avatar of CodingExperts
CodingExperts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of widiyantosg
widiyantosg

ASKER

ok thanks.. actually I got the solution already. Its because I get the wrong field number. hehe.. but I'll give the point anyway..Thanks