Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieve image from database

 
P Arunkumar
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Travis Berthelot
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where i wants to give this code, please saw my first post that is my code iam having.
 
Jo Joseph
Greenhorn
Posts: 26
 
Jo Joseph
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using the jsp/html for sending the request.. rt?
While "submitting" the url need to be appended.
 
P Arunkumar
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P Arunkumar wrote: please solve my issue.
post the code
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P Arunkumar wrote:its so urgent please

Please EaseUp. There is no such thing as "urgent" around here.
 
P Arunkumar
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please answer the questions I posted in my reply.
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i didn't get your point , what image servlet
 
Paul Clapham
Sheriff
Pie
Posts: 20771
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't design any page to return an image as its response.
 
P Arunkumar
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic