Displaying blob on jsp

Posted on 2004-11-22
Last Modified: 2010-04-01

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");
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body bgcolor="#F0F0F0">
<form action="form1" method="post" enctype="multipart/form-data">
<table width="45" height="44" border="1">
    <td width="36"><img src="inventory_action1.jsp?action=viewImage&id=<%=id%>"></td>

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") ;
byte[] bytearray = new byte[4096];
int size=0;
InputStream sImage;
sImage = rs.getBinaryStream(4);
while((!= -1 )

can anybody tell me wat is the error? Thank you
Question by:widiyantosg
    LVL 6

    Assisted Solution

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

            // 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= != -1 ) // Read from file
              blobOutputStream.write(buffer, 0, nread);         // Write to Blob

            // Close both streams

            // 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 = != -1) //Read from Blob stream    
              fileOutStream.write(nbuffer, 0, nbytes); // Write to file stream
    LVL 19

    Accepted Solution

    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...

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    Apache server configuration 7 69
    Fisheye tool 2 96
    squirrelPlay java challenge 40 155
    withoutString  challenge 40 119
    Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
    If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now