aspose file tools*
The moose likes Struts and the fly likes image storing and displaying (struts2 + hibernate) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "image storing and displaying (struts2 + hibernate)" Watch "image storing and displaying (struts2 + hibernate)" New topic
Author

image storing and displaying (struts2 + hibernate)

Nemanja Kovacevic
Greenhorn

Joined: Aug 27, 2009
Posts: 5
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...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 27, 2009
Posts: 5
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

Joined: Aug 27, 2009
Posts: 5
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

Joined: Aug 27, 2009
Posts: 5
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'll want to ask Hibernate-related questions in the ORM forum.
Nemanja Kovacevic
Greenhorn

Joined: Aug 27, 2009
Posts: 5
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
vic ming
Greenhorn

Joined: Sep 15, 2013
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: image storing and displaying (struts2 + hibernate)