Spring JDBC
I have added a image into My Database! But Now I don't know how to retrieve it from database and show it on my frontend. Can Anyone Help ?
66 Replies
⌛
This post has been reserved for your question.
Hey @Danix! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
How did you add the image to your DB?
Using MultipartFile Library
@dan1st | Daniel
Can you show the relevant code?
Yehh sure
Here is the controller
@dan1st | Daniel
That doesn't store the bytes anywhere, right?
no it stores the bytes in database
or does
finderService.addUser()
do that?
How?from this
retrieving probably works somewhat similar to storing
Now what's findUserImp?
its the implemented class of dao interface
@dan1st | Daniel
Can you show that?
What does
finderUser.getImage()
return? A byte[]
?Where ?
yehh yeeh
then you can probably use Spring JDBC to execute a SELECT statement
and that can get the
image
as a byte[]
u mean like this
@dan1st | Daniel
yes
and then how to convert the byte into string for the proper image ?
why convert it to a String?
bcz what the data is coming is in byte form and i want it to be a image !
well images are binary data
i.e. bytes
you are also using a byte[] here
yehh
bro tell me what to do ? @dan1st | Daniel
Just use the byte[]?
byte[] seems fine to me
hmm like i succesfully getting the bytes of the image but now what to do to convert it from byte to a inage ? thats my question
What format do you want for the image?
JPEG or Png Anything
it is a jpeg or png or whatever stored in a byte[]
its a jpeg that was stored in byte []
So what's the issue about that?
i think u dont get my question i mean i succesfully decode the image to byte from my DB alright and now i want to show that image which i decode into byte in my jsp page then how could i do it ?
oh ok
yehh
you could convert it to base64 and include it in the JSP
or you could create another endpoints, one for getting the image
and the JSP has a <img> loading the image from that endpoint
i have to convert it into base64 in my controller or my jsp page
Base64.Encoder (Java SE 21 & JDK 21)
declaration: module: java.base, package: java.util, class: Base64, class: Encoder
Successfully decode it but getting this in the img tag when checking it from devtools .. and the image is not there as well
@dan1st | Daniel
My Jsp
is
image
the base64? stringyehh
here is the controller
and it isn't the empty string?
the profile
ok done i got it @dan1st | Daniel
Can you try viewing
image
there using a debugger?
Does it work?heyy i am getting one more doubt that like i want to fetch the third party API and i succesfully fetched it from restTemplate but how to convert the JSON into String Object can i use GSON library for it ?
yehh i was retriving it in the wrong way ! i was retriving it in the register Controller where it was saving not in the loggedIn controller where the query is actually working :hidethepain:
you can - or you could use something else
This is what i have done Service annotation is good for it or not and where could i use the GSON library at the same class or i can make the diferent class
?
@dan1st | Daniel
I think RESTTemplate can convert to objects if wanted
do u have any doucmentation related to it
Hey I Suddenly Starts to get this
@dan1st | Daniel
Does your program run twice?
now i have already closed it and closed the ecplise as well
and after that i got this
Try to restart your computer.
(if the tomcat run as a windows service you will have this problem after restart too)
try this
The jsp-part might look like this:
<img src="profilePhoto.jpeg?userId=${user.id}" />
A
userId
parameter to the GetMapping
?Yes, considerations?
How should Spring know what to put there?
It is automagically gathered from url-parameter
which sucks for security
like the user can literally decide what user to get information about
(Also you didn't use annotations to tell Spring about that)
For url-parameters you dont need a annotation. What else annotation should I need?
Neither part of the question nor common practice.
Xing publishes all profile-fotos, facebook, gulp, etc. all offer everyones profile photo.
@RequestParam
by default compilation doesn't include parameter names btw
these are normally erased/not included in the class fileGood point.
You did that in your code
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.