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
widiyantosgAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodingExpertsCommented:
A sample program for read and write of blob... see if it helps


    if(rs.next())
    {
        // Get the Blob locator and open output stream for the Blob
        Blob zipBlob = rset.getBlob(4);
        OutputStream blobOutputStream = ((oracle.sql.BLOB)zipBlob).getBinaryOutputStream();

        // Open the sample file as a stream for insertion into the Blob column
        File imgFile = new File("c:\\image.bmp");
        InputStream sampleFileStream = new FileInputStream(imgFile );

        // Buffer to hold chunks of data to being written to the Blob.
        byte[] buffer = new byte[10* 1024];

        // Read a chunk of data from the sample file input stream, and write the
        // chunk to the Blob column output stream. Repeat till file has been
        // fully read.
        int nread = 0;   // Number of bytes read
        while( (nread= sampleFileStream.read(buffer)) != -1 ) // Read from file
          blobOutputStream.write(buffer, 0, nread);         // Write to Blob

        // Close both streams
        sampleFileStream.close();
        blobOutputStream.close();


        // Open a stream to read the Blob data  
        InputStream blobStream = zipBlob.getBinaryStream();

        // Open a file stream to save the Blob data
        FileOutputStream fileOutStream = new FileOutputStream("c:\\img1.bmp");

        // buffer holding bytes to be transferred
        byte[] nbuffer = new byte[10];
        int nbytes = 0; // Number of bytes read


        // Read from the Blob data input stream, and write to the
        // file output stream  
        while((nbytes = blobStream.read(nbuffer)) != -1) //Read from Blob stream    
          fileOutStream.write(nbuffer, 0, nbytes); // Write to file stream
         
        fileOutStream.close();
        blobStream.close();
    }
0
KuldeepchaturvediCommented:
try shortening your buffer size to 1k and see if it fixes your problem.. I have seen in happening earlier so to avoid I always prefer to read the stream byte by byte...
Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
widiyantosgAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.