aspose file tools*
The moose likes JSF and the fly likes JSF uploaded file location Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF uploaded file location" Watch "JSF uploaded file location" New topic
Author

JSF uploaded file location

Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26
Dear all,
I used <t:inputFileUpload > to upload file with JSF.
but I don't know the location of uploaded file ?
My problem is: I need to read a data from an excel file. So I need to upload an excel file to server
then read the its content. But I do not know the location to read.
Would you please show me the solution
Thanks so much
Phuc Bui
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

1. Create a property of type "UploadedFile" - I think that would be in package org.apache.myfaces.tomhawk.

2. Create setters and getters for it.

3. In your action processor, you can use the methods defined in http://myfaces.apache.org/tomahawk-project/tomahawk/apidocs/org/apache/myfaces/custom/fileupload/UploadedFile.html to access the file data and metadata.

You should be aware of a couple of things, though. First, the "file name" isn't the actual name and location of the uploaded file. It's the name sent from the client. Properly speaking, it should just be the filename itself, but apparently some versions of Internet Explorer also send the user's local filesystem path, which is A) useless to the server, since the server can't access the client's local files - only accept files the client explicitly sends. B) Dangerous, since the more outsiders know about the user's local computer setup, the more they can exploit it. C) Not portable, since Firefox plays by the rules and doesn't send paths, so whether or not you get a simple filename or its full client absolutepath depends on what browser you use. So if you plan to actually use the uploaded filename, it's probably better to do this:



D) Some frameworks may automatically correct for Microsoft's little joke, but I don't know if Tomahawk is one of them.

If you look at the Javadocs for UploadedFile, you'll notice that there are methods for getting the contents of the uploaded file, but not the actual "file". That's because when you upload a file, it's serialized and sent as a sequence of MIME-encoded characters to the server, which then MIME-decodes the data. Although the uploaded data may be stored in a file on the server, there's no guarantee that it is so - only that the data itself will be available for you to use (it could be stored in RAM, for example). So if you want to place this data in a server-side file, you have to use the UploadedFile's getBytes or getInputStream method to obtain the data and write it out to a server-based file that you create yourself.

Whoops - almost forgot. If you absolutely positively must use the whole filepath from the upload and not just the filename part, please remember that Unix (Solaris, AIX, etc.) and Linux don't have "C:" drives. So it there's even a remote chance that your code will run on a system other than Windows, handle it accordingly.

It's not very safe to mimic some other machine's filesystem structure on your server, though.

Of course, you can just as easily take that data and put it into a database BLOB/CLOB or even feed it directly into an XML SAX parser, if that works better for the job you're doing.

Customer surveys are for companies who didn't pay proper attention to begin with.
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26
Thanks Tim,
That's helpful
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26
Dear,
Would you please tell me how to define UploadedFile object in Spring >
I tried this

and my bean using it


But the UploadedFile object is always null ?

Thanks,
Phuc Bui
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

It's probably an interface, actually. But that's not what you need to do. You need setter/getter methods on a backing bean for the UploadedFile object. Tomahawk is what creates that object - not Spring. It will create and inject one any time you actually upload a file.

Spring is more suitable for permanent objects - primarily singletons when you're talking JSF, where most of the multiple-instance objects tend to be managed beans.
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26
Hi,
I have already had getter and setter for UploadedFile object
Tomahawk is what creates that object - not Spring.

So you mean, I do not need to declare anything about UploadedFile in Spring ?
But how about faces-config

How should I do with <managed-property> for the property uploadedFile ?

Thanks,
Phuc Bui
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

No, once again you're thinking of a long-term life for a short-term object. The value attribute of the tomahawk indicates where the UploadedFile object will be injected.

Just to clarify - the UploadedFile object isn't something of value itself - it's a temporary holder of information about data that has been uploaded by the fileupload tag controller.

There are some good examples on how to use Tomahawk online, but they weren't on the actual Myfaces website, they were on a third party site that if you looked carefully you could find a link from on the MyFaces site. However, the MyFaces site was recently overhauled and I'm not sure where the best place to find working examples is, now.
Sorcha Fooskle
Greenhorn

Joined: Aug 03, 2010
Posts: 3
Hi Tim,

I've checked J2SE 1.4, 5 and 6 for the getFileName method of File and all only have getName method... Assuming that that's what you meant, I tried your suggestion.

It doesn't work in my scenario, because client runs on Windows; server on Linux. Because the file.separator system property on Linux is / and presumably \ on Windows, full path is still returned by getName.



Just to make others in same situation aware...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF uploaded file location