aspose file tools*
The moose likes JSP and the fly likes Storing and Retriving Images from DB(Oracle) using JSP/Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Storing and Retriving Images from DB(Oracle) using JSP/Servlets" Watch "Storing and Retriving Images from DB(Oracle) using JSP/Servlets" New topic
Author

Storing and Retriving Images from DB(Oracle) using JSP/Servlets

Pankaj Jain
Greenhorn

Joined: Mar 04, 2002
Posts: 3
Hello
I saw several hints and I tried sevaral ways to save/display images to/from the blob field.
But I could not succede, while trying to insert the image I am not able to insert the image more than 4KB size.
Can any one help me on this issue. Please send a complete working example(jsp/servlet etc) if you have any.
Thanx in advance
[ March 05, 2002: Message edited by: Pankaj Jain ]
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
Perhaps I should start a new thread on this, but I don't understand the benefit of storing images in a database. I have always stored only the path to an image resource, never the actual image. Does your project *require* storage in the db? If so, why?
...just curious...
If your'e working with Oracle, it appears they have some of this functionality built in:

See: Oracle Docs
[ March 05, 2002: Message edited by: Brian Glodde ]
Pankaj Jain
Greenhorn

Joined: Mar 04, 2002
Posts: 3
Hello Brian
Thanx for taking time to reply. The reason that I am storing the images into database is that :
1) its client's req.
2) Looks in future the client would like to upload the images from the website into the database. This way the task is secured and over.
I will appriciate your efforts if I can get a sample jsp and servlet/java code to interact with.
As I am pritty new to java/jsp this will help me lot to understand.
Thanx a lot once again
Bye
Jain
Originally posted by Brian Glodde:
Perhaps I should start a new thread on this, but I don't understand the benefit of storing images in a database. I have always stored only the path to an image resource, never the actual image. Does your project *require* storage in the db? If so, why?
...just curious...
If your'e working with Oracle, it appears they have some of this functionality built in:

See: Oracle Docs
[ March 05, 2002: Message edited by: Brian Glodde ]
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
Here are a few URLs with code examples that will help:
>> Uploading a file to a BLOB
>> Retrieving a BLOB
>> BLOB performance
>> Blob problem
Pankaj Jain
Greenhorn

Joined: Mar 04, 2002
Posts: 3
Hello Brain
Thanx a lot once again for the guidence you gave.
With your tips and help I was able to write a piece of code, but I am unable to display the JPG image in my JSP page. I read the image and send the binary data to the ServletOutputStream.
I am including the piece of code. Please let me know where I am commiting the mistake.
Class.forName("oracle.jdbc.driver.OracleDriver");
String str_url = "jdbc racle:thin:@gops_test:1521:GOPS";
Connection con = DriverManager.getConnection(str_url,"gops","gops");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM TeeColor where TCOLOR='Black4j'");
if (rs.next())
{
String dim_image = rs.getString("TCOLOR");
//byte [] blocco = rs.getBytes("TCBLOB");
response.setContentType("image/jpeg");
ServletOutputStream op = response.getOutputStream();
///////////////
Blob blob;
blob = rs.getBlob( "TCBLOB" );
System.out.println("TCBLOB");
int iLength = (int)(blob.length());
op.write(blob.getBytes( 1, iLength ));
/////////////////
}
rs.close();
stmt.close();
con.close();
} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}
in the above example I am for sure that it reads the image, it shows me the size of the file, but on the page I can see the image icon, with no image display.
Wish I will be able to solve the problem.
Thanx once again.
Bye
Jain
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Recall that an image in a HTML page is fetched by the browser based on an <img> tag not served in the middle of the HTML by JSP. What does your <img> tag look like?
Bill
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
One approach you might try is to write a servlet using the code you have given to serve up the image, with a request parameter requesting the specific image you want, and then use the src attribute of the <img> tag to call your servlet.
For example:
<img src="myServlet?image=Black4j">
Of course you could also do this by writing a custom tag, but the servlet will work fine as well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing and Retriving Images from DB(Oracle) using JSP/Servlets