• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSF uploaded file location

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Phuc Bui
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim,
That's helpful
 
Phuc Bui
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic