JSF backing bean access to web tier directories
I think my previous post on this was too complicated and wordy. Maybe this will help me get an answer
In my JSF backing bean, I'm plucking BLOBs from a MySQL database. I need to write them to files in my web tier directories.
If I do a current directory check from my backing bean, it says the current directory is Tomcat, but depending on the deployment, this may or may not be the case. I mean I may deploy it on WebLogic or WebSphere or JBoss. My web tier directory structure looks something like this:
public_html
WEB-INF
images
scripts
documents
tomcat
I need to put the BLOBS into the public_html/images directory.
Does anyone out there know how to properly access web tier directories from a JSF backing bean?
If I just open a file without a path, they surely do go into the tomcat directory. Attempts to open a file ../public_html/images/filename.ext have met with exceptions.
This works perfect. I would like too add a few bits of code that took me
a while to get to.
I have an entity called Product, with a property "image" - "image" contains
the name of the image that's associated with it. I could have made "image"
a blob type, but I just didn't feel like going that route.
So, in my backing bean, I have the following (getter/setter, and a helper function):
(i didn't have time to clean up a few statements in the code, but you get the idea.)
public UploadedFile getMyUploadedFile() {
return myUploadedFile;
}
public void setMyUploadedFile(UploadedFile myUploadedFile) {
this.myUploadedFile = myUploadedFile;
product.setImage(getUploadedFilePath());
}
public String getUploadedFilePath() {
BufferedImage image = null;
String name;
RenderedImage ri;
File imagefile;
String realPath;
name = "";
try{
realPath = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("/images/content/");
image = ImageIO.read(myUploadedFile.getInputStream());
name = java.lang.String.valueOf(java.util.Calendar.getInstance().getTimeInMillis());
name = name + ".jpg";
imagefile = new File(realPath + "/" + name);
ImageIO.write(image,"jpg",imagefile);
}catch(IOException e){
name = null;
}
return name;
}
Since I use tomawalk (search the NetBeans mailing list on my resolution
to installing tomahawk into NB, ie "nbusers"), I have the following in my
JSF page:
<%@taglib uri="http://myfaces.apache.org/tomahawk" prefix="t" %>
<h:form enctype="multipart/form-data" >
<t:inputFileUploadid="produtIcon" storage="file" accept="image/*" value="#{product.myUploadedFile}"/>
The only think I haven't figured out is how to pass the image data to a
hashing function to append to the timestamp for the image name. I'd
don't know how else to ensure a unique name for my image file.
Good luck to the next person!
Message was edited by:
groverblue