aspose file tools*
The moose likes Servlets and the fly likes Retrieving image file from the database and displaying in the browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Retrieving image file from the database and displaying in the browser" Watch "Retrieving image file from the database and displaying in the browser" New topic
Author

Retrieving image file from the database and displaying in the browser

reddy palwai
Greenhorn

Joined: Nov 06, 2002
Posts: 11
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Aug 17, 2001
Posts: 399
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

Joined: Aug 19, 2000
Posts: 184
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

Joined: Nov 06, 2002
Posts: 11
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

Joined: Aug 17, 2001
Posts: 399
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Retrieving image file from the database and displaying in the browser