image storing and displaying (struts2 + hibernate)
Nemanja Kovacevic
Greenhorn
Joined: Aug 27, 2009
Posts: 5
posted
0
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...
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
posted
0
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
posted
0
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;
}