• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

image storing and displaying (struts2 + hibernate)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new on this forum so first I would like to greet you all.

I'm bulding struts2 + hibernate + tomcat application and now I have a problem.

Users should be able to upload their photos and later that photos should be visible on their profile page.

Uploading a file in struts2 is , as we all now, super easy so I find myself with a File object in my Action class.

What should I do next? My first idea was to store a file on images folder in my web app root so I can easily reference it from html later but I can't find path for that. Second idea is to put it in database as blob or something but how to display it later?

Any ideas, examples? It's a bit surprising for me that googling didn't provide any answers because it seems like a common scenario...
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not a big fan of storing images in the DB, but it'd work. I prefer to keep them in a non-web-app directory. In Struts 2 they can be served back using the "stream" result.

What exactly are you having problems with?
 
Nemanja Kovacevic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying both solutions but I can't get them to work.

OK, I get a File object from FileUploaderInterceptor and I can open an InputStream and read those bytes. Now I can create an OutputStream and write those bytes to a file on my server. I would very much like to create that image file in my webapp/images folder, so I can referense it from html. But I can't get to that path in Tomcat, all I can get is Tomcat temp dir and I can't store my images in a temp dir. You say you prefer to keep them in a non-web-app directory, do you meen like on C:/images? If so, how can I refernce them from html later? Or should I create a JSP page with action tag that has stream result and returns my image? If so where to put my image files?

Thanks for your effort.

 
Nemanja Kovacevic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for db variant, I can't get the file to save to DB.

my Image class:


public class Slika implements Serializable{

long id;
Blob blob;

public Blob getBlob() {
return blob;
}

public void setBlob(Blob blob) {
this.blob = blob;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

}



hibernate maping:


and test action code:


There is no exception, Hibernate logs insert statment but no row in DB?

 
Nemanja Kovacevic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UPDATE:

Now I can successfully sotre and retrieve my image from DB and even display it from struts action.

Action code:


As a result I get a picture displayed in my browser. But I need to include that image in other content. Now I'm trying to to that.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll want to ask Hibernate-related questions in the ORM forum.
 
Nemanja Kovacevic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer is to call your action as a resource from html img tag

like this

<img src="/WebApplication_1/administrator/PrikazSlike.action" />

working with previous action code this will inject image on your web page
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As i have also search for the same topic but i find out relevant one but they have used ( JDBC + Struts2 ) ......
but you can do all the jdbc connectivity in hibernate that is very easy as we have to use .xml file for database connectivity

else hibernate they have done all the thing like storing image and retrieving it back with struts2 and jdbc.
they are also providing full code to download without any registration just click here to reach the page
and download full code means with struts2 and oracle 10g connectivity jars

helpful for me...may be for you all
 
Do Re Mi Fa So La Tiny Ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic