Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving image file from the database and displaying in the browser

 
reddy palwai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, here is my problem
I need to insert and retrieve the image file from the database.
I am using http://www.servlets.com/cos/index.html
package for dealing with multipart request when uploading the file.
I am then inserting the file into the database.
I think it is inserting the file properly into the database.
now I need to retrieve the image blob from the database and display it in the browser.
here is my servlet code :

import java.util.*;
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class blobview extends HttpServlet
{
private BLOB myblob;
private String mname="";
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("image/gif");
Connection con=null;
Statement stmt =null;
ResultSet resl=null;
String flname=req.getParameter("fname");
try
{
DriverManager.registerDriver( new oracle.jdbc.OracleDriver());
con =DriverManager.getConnection("jdbc racle ci8:@test1","rpalwai","rp6701");
con.setAutoCommit(false);
stmt = con.createStatement();
resl = stmt.executeQuery("SELECT NAME, BFILE FROM SAMPLEBLOB WHERE NAME = '" + flname + "'");

while( resl.next() )
{
//mname=resl.getString("NAME");
myblob = ((OracleResultSet)resl).getBLOB ("BFILE");
InputStream blobStream = myblob.getBinaryStream();

int chunkSize=myblob.getChunkSize();
byte [] byteBuffer=new byte[chunkSize];
String saveFile=targetDir + flname;
FileOutputStream fileOutStream = new FileOutputStream(flname);
int bytesRead;
while((bytesRead = blobStream.read(byteBuffer))!= -1)
{
fileOutStream.write(byteBuffer);
}
fileOutStream.close();
blobStream.close();
}
}
catch (SQLException e)
{
throw new IOException("Database Error querying insert new file"+e.getMessage());
}
finally
{if(stmt!=null)
{
try
{
stmt.close();
con.close();
//con.commit();
}
catch(SQLException e)
{
}
}
}
}
}
This code does not work. it just displays an image icon not actual image.
please help
thanks,
reddy
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
well, i have never done anything like this before so i can't give any pointers but here is what i noticed in one of the projects (it was a GIS project),
-we dont store an image into the database
-we store hyperlink to the images in the database
-upon retrieving the hyperlink we download the image
this might be helpful in cases like GIS projects where we have lot of Geographical data in terms of images and we simply can't afford store them in database OR we are utilizing already existing images on web, if you know what i mean..
just my 0.00002 cents i would say as this doesn't at all address question you posed...
regards
maulin
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
got to agree with maulin...
storing image in db would put lot of load on database, instead just store the url or the file name & cud be retrieved whenever you would like to use, i have done the same thing many a times, never liked the idea of storing the image in a database.
malhar
 
daman sidhu
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I had done the very same thing in my last project.. after doing some R&D on blob i found a better option was to use long raw instead.. store the image in the database as long raw and just read and write it as byte array and display as it is on the browser.. ull get the desired results too and very less overhead as you wont have to deal with blob objects..
Cheers!!
Daman
 
reddy palwai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the suggestions. I'll try storing the images as longraw or store just the hyperlinks.
Is there a way to read all the files from a particular directory?? People can ftp their image files to this directory and a servlet can retrieve all the files as images from this directory.<img src="images/hi.gif">
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by reddy palwai:
Thanks for all the suggestions. I'll try storing the images as longraw or store just the hyperlinks.

Is there a way to read all the files from a particular directory?? People can ftp their image files to this directory and a servlet can retrieve all the files as images from this directory.<img src="images/hi.gif">

well
if you are going to store the links in database you can surely retrieve it from there...i cant think of anyway through a servlet...leave that to other greehorns...
hth
malhar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic