File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes retrieve image from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "retrieve image from database" Watch "retrieve image from database" New topic
Author

retrieve image from database

P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
Hi

I wants to retrieve image from database and the image wants to appear in browser. Please help me to solve my issue. But my code is worked in first time after that it is downloading as .jsp file whats problem please solve it.Its so urgent i am in edge please solve my issue.

DisplayImage.java




But while iam running in first time its work fine but from second time its downloading pleasee help me my photo wants to appear in browser please solve my issue.


Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

You're going about it the wrong way, for a few reasons:

1) Don't use getString for a binary field to get its length. Binary data like an image is not a String.
2) Don't use InputStream's read(byte[]) or read(byte[], int, int) method without a loop. The method is never guaranteed to read exactly as many bytes as you want.

In your case, I would go for one of two ways. Both will do only one single query.
1) Use getBytes to get the entire content as a byte[], then write that to the response output stream.
2) Use only getInputStream, then read from the InputStream until the read method returns -1. Here is an example on how to do that.

Also, make sure you close the ResultSet in a finally block. If something goes wrong in your current code the ResultSet won't be closed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Travis Berthelot
Greenhorn

Joined: Oct 30, 2004
Posts: 24
Don't use empty exception blocks.
Don't create strings for each request.
Don't load the driver more than once.
I would start with that first.
Jo Joseph
Greenhorn

Joined: Nov 28, 2010
Posts: 23
Whenever we retrieve the image from the DB. It will be downloaded to the system. Then next time whenever
we sends the request for an image the browser will look whether there is any downloaded image and it will show the
image. So at the time of second request append some random value to the url. Then browser will try to fetch a new image.


you can refer Retrieve image from database using Servlet in Rose India for the code.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
Thanks for reply,


Please can you provide some sample code to guide me. Please help me, its so urgent please , today i wants to finish my work , please no one is not there to help me, iam developing alone , please help me.
Jo Joseph
Greenhorn

Joined: Nov 28, 2010
Posts: 23
if this is the original url
http://server/myapp.php?id=10

just add
http://server/myapp.php?id=10&x='+(Math.random())

in this case every time some new random no will be added to the url and the browser
will take the request as the new one.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
Where i wants to give this code, please saw my first post that is my code iam having.
Jo Joseph
Greenhorn

Joined: Nov 28, 2010
Posts: 23
http://www.roseindia.net/servlets/retreiveimage.shtml
Jo Joseph
Greenhorn

Joined: Nov 28, 2010
Posts: 23
Are you using the jsp/html for sending the request.. rt?
While "submitting" the url need to be appended.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
I am using Jsp+ servlet, Iam getting Student Register number, in photo place student image wants to appear ,all the text details coming perfect except photo, i saw
http://www.roseindia.net/servlets/retreiveimage.shtml but the same code is getting download the jpg file as.jsp file please solve my issue.
vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 81

P Arunkumar wrote: please solve my issue.
post the code
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

P Arunkumar wrote:its so urgent please

Please EaseUp. There is no such thing as "urgent" around here.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
My code is in first post please go up and see, really in first time i saw the photo in my browser but it is not working for next time.. please solve my issue
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61772
    
  67

P Arunkumar wrote:My code is in first post please go up and see

Please repost the code with proper indentation and UseCodeTags. Unindented code is too hard to read.

Also, what is happening on the second time? Is the servlet even running? ShowSomeEffort and find out as much as you can about how the two passes differ.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

P Arunkumar wrote:but the same code is getting download the jpg file as.jsp file please solve my issue.


I'm sorry, I have no idea what this is supposed to mean. Could you explain it differently, perhaps with more detail?
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
I have stored some images in my database.in my database iam have id and image fields. id field varchar and image type as blob. now my task is i wants to retrieve the image from database. and the image wants to appear as output, because my project is to give student hall ticket iam right hand side image wants to appear.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61772
    
  67

Please answer the questions I posted in my reply.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

You haven't said anything about the HTML which contains the link to this image servlet. Could you show that to us?
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
what i didn't get your point , what image servlet
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

P Arunkumar wrote:what i didn't get your point , what image servlet


The code you wrote is an image servlet. It's called that because it's a servlet which is designed to return an image as its response.

It seems to me that part of your problem is that you are having trouble with English. It doesn't make it any easier to ask questions about technical subjects which you are trying to learn, when you can't even find the words to ask the questions. Could I suggest that you find somebody who speaks English a bit better, and ask them to help you ask the questions?
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
iam having one more idea please tell me it will be possible or not. by using some condition i can open image from local system because iam going to give demo in mylap please if possible or not please me
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
I didn't design any page to return an image as its response.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 119
To return an image as its response how i wants to create html page please iam beginner, if iam doing some mistake please guide me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieve image from database