• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Retrieve a image stored as blob in a database and display it on a web page

 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to store an image in the database and then i need to display that image on the webpage using spring mvc hibernate.
What approach i should follow?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What particular issue are you facing implementing that? A BLOB data type suggests itself for DB storage.
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the matter is not related to storage, its about how to retrieve image from the database and display on the web page using the hibernate.
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are able to store the picture as BLOB in the database, then, you can use any of the approaches to display the image.
1. use the controller to retrive the BLOB and create a image inside your webapps/images (or any folder) and pass this path to JSP to display the image.
2. stream the image in jsp, using the tag.
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Prasads
can you show me some examples for the first approach. actually i am first time using the springs and hibernate and don't have idea of syntax for retrieving image using hibernate and how to display that on the jsp page.
 
Prasad Krishnegowda
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google is your friend. Try to start with the approach.
One example which i found just by searching for blob spring and hibernate is this, BLOB-SpringMVC-Hibernate Example.

Try understanding the solution and implement it, if you any questions/doubts please come back and post here.
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i searched alot but i am getting the example with maven. i don't have to use maven.
i can just use springs and hibernate.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you will use some build tool - why not use Maven to start with? Its use or non-use is not related to the issue at hand, though.
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can't use maven because i am working on one of the module and others are doing their modules without using maven so it can creates problem while integration.

please help me.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, the choice of build tool is not germane to the problem. If you're using Ant, or Gradle, or some other tool, that has no bearing on the solution. You will have to adapt the tutorial for that tool, obviously.

If you're saying that that's what you're struggling with, then I think you may have bitten off more than you can chew at the moment. In that case, start with a simpler case, mybe create a desktop app that reads a file, stores it as a BLOB in the DB, and then reads it back and creates a new file from it. Leaving out the entire web app aspect simplifies things a lot, and will learn the file handling and Hibernate parts of it.
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not understanding from where i should start.
not getting the single example code that is working with hibernate
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe try a little harder? If I search for "hibernate blob tutorial" I get plenty of relevant results.

And I still think you could just as well follow the tutorial Prasad linked to. Since you're just learning, the choice of build tool really does not matter. After you have learned how this works, you can always adapt it to whatever build tool your team uses.
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i too got many results but none of them is workable
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, just throwing your hands up in the air and saying "this stuff is too hard" won't get you anywhere either.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Megha,

If you are looking for someone to give the code to you, you are in the wrong place.

If you want to serve up an image through a servlet, what you do is retrieve the contents of the blob into memory as a byte array, and then write it to the servlet's output stream. Also set the request's content type to match the content type of the image. So, if the image is a JPEG, you have to set the content type to image/jpg

If you are using spring mvc, add HttpServletRequest and HttpServletResponse as parameters to your handler method. Spring will automatically pass you the request and response if it sees it in the method

Btw, are you already using spring mvc, or are you looking at spring mvc just to serve the image from database. I wouldn't recommend adding spring mvc just to serve the image. It's as easy to do it in a servlet.
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not throwing hands. its like i have tried every example available on the internet but none of them is working.
at end i posted this problem.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Figuring out why something doesn't work is the essence of debugging; surely everyone can use a bit of sharpening their skills at that. If you have concluded that examples others have come up with aren't your style, then you can start off with the basic Hibernate app I suggested earlier.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. A lot of young programmers tend to think that programming means googling around for solutions, copy-pasting them when you find it , and getting stuck when you don't find a solution. To many of us old-timers that is the very definition of throwing up hands. Programming doesn't mean googling and copy-pasting. It means understanding the tools and techniques, and then figuring out a way to make those tools and techniques solve a specific problem. There was a time when there was no blogs, no Stack Overflow. Heck there was no Google, and we had to learn an API from a book... A BOOK!! Eventually, at some point in the future, the people who figure stuff out and put them on blogs are going to either retire or die, and then there will be no one left to write the blogs that you can copy-paste from.
 
Prasad Krishnegowda
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Megha, Perhaps you can show us what you tried and what error/exception is coming, just telling its not working, is a vague statement and as jayesh pointed, its like throwing up your hands..
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following code displaying image on the web page but not uploading it in the folder
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"home/meghas/images" is apparently a directory, correct? If so, line 21 can't possibly work.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hold on you are confusing me now. I thought you wanted to retreive an image from a database and show it to the user. But the code that you posted allows the user to upload a file that you save to a folder on the web server

How are these 2 things related?
 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no actually previously i was thinking to insert image directly into database but now i decided to insert image path in the database and image on the system as i couldn;t find out how to insert value in the database using spring hibernate
but this i have done through servlets previously so i am now trying that.

at line 21 what is the error?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

at line 21 what is the error?


It opens a FOS to a directory, not to a file. (Assuming that "home/meghas/images" is a directory, which you haven't confirmed.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic