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.

[958 byte] By [JbuilderDouga] at [2007-10-3 6:46:05]
# 1
BTW, in the above post, public_html and tomcat are at the same level. All other directories shown are down one levelpublic_html|WEB-INF|images|document|scriptstomcat
JbuilderDouga at 2007-7-15 1:36:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I don't have a clean solution to your problem except maybe create a properties file with the direcoty in which to write the file. Not what you are looking for though. Do you have to write the blob to a file. What I did was read the BLOB data from the DB and displayed it the Web Browser. If this option can work for you I will be more than happy to supply you with the code to do it.

ferric4a at 2007-7-15 1:36:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
String imagesPath = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getRealPath("images");
BalusCa at 2007-7-15 1:36:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
PERFECT!!!!!!!!!!!!Fantastic. I hope this answer helps other also who must, at some point ben wondering about the same thing.
JbuilderDouga at 2007-7-15 1:36:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

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

groverbluea at 2007-7-15 1:36:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...