[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Displaying blob on jsp

Posted on 2004-11-22
3
Medium Priority
?
611 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:widiyantosg
3 Comments
 
LVL 6

Assisted Solution

by:CodingExperts
CodingExperts earned 300 total points
ID: 12651870
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
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 75 total points
ID: 12652047
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
 

Author Comment

by:widiyantosg
ID: 12652161
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question