• 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

How to read a CSV file from a user defined directory path for loading to Managed Bean

 
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm probably missing something painfully obvious here. I'm using a PrimeFaces UploadedFile xhtml page to select a csv file to read and write using a managed bean (SuperCSVParser.java). The file is read and written to an entity class which then persists the data to a database. The application works fine if I specify a file path on the physical server and select a csv file on that file path. But for the production version I want the user to select ANY file name from ANY directory on their local system.


I know about the FacesContext methods and I've looked at some methods from the java.io File class. Most of these methods are about getting the path from the server, where I want to 'pass' the path String from the client machine to allow the uploaded file to go through. When I try with the below code I get:




I'd like to know what I'm doing as I prefer not to explicitly declare a path in the final app. I'm almost sure that's possible. Thanks in advance!







 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jay

For reading a PrimeFaces UploadedFile, you can use the following to the file name:




For web uploads, the file goes to some temp directory (you wouldn't usually know) then once the file is uploaded to this path, your code will run. Hence the UI may hang during the upload for large files. So you may want to put your code to run in a thread.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's great. Thank you very much. I am having some issues running the app as a thread but I will keep trying and update my progress. Thanks again!
 
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
HTTP File Uploads are not literally file connections.

What actually happens is you will have defined an HTML FORM with a INPUT control whose type is "file". JSF/Primefaces will have rendered this for you. The FORM type must be of type MIME/multipart, which tells the browser to use MIME encoding on the (possibly not-text) upload data and that there can be multiple data items being uploaded, not just the file itself.

As I said, there's no over-the-internet file operation going on here. What actually happens is that the client (browser) will take the selected file (normally it will pop up a local file browser dialog to allow you to select which file you want), it (the client) will open that file, read it, convert it to MIME encoding, and stuff the encoded data in as part of the overall data stream being submitted over the network to the server.

Once this complex URL request lands on the server, the server routes it to the appropriate webapp where the backend support logic breaks it out to its components and routes the components to their appropriate destinations. For simple controls, that means HttpRequest attributes. For the file data, that means it gets cached somewhere, most likely in a temporary file or in RAM and probably will be MIME-decoded back to its original bytes.

Where that cache is and what its organization and format are are none of the webapp's business. The file upload API (as fronted by the PrimeFaces JSF fileupload control API) knows where to get the data - and its metatdata - when you request it and that's all the app needs to know. The app can then open the data as a stream and copy it to an actual file on the server's filesystem, stuff it into a database BLOB, consume it directly in application logic or do whatever the application programmer wants to do with it. The app logic itself would normally be invoked from a JSF "action" method or AJAX listener.

One thing to note is that when a "filename" is sent as part of a file upload over HTTP, the actual "filename" is determined by the client and doesn't actually have to literally be the client-side filename. In real life, Internet Explorer tends to send the entire filesystem pathname, Firefox and other browers tend to send the simple filename without path. Technically, the IE was is a security risk since it tells outsiders how the client's disk drive is organized and that's not essential information, especially since the server cannot access the client-side file directly in any event and therefore doesn't need to know its path.
 
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
Oh, and BTW, DON'T dissect upload filenames by looking for backslashes. Unix/Linux/MacOS doesn't use them.

The portable way to get the base filename from a path is like this:

 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Oh, and BTW, DON'T dissect upload filenames by looking for backslashes. Unix/Linux/MacOS doesn't use them.



Depends on what browser you use. For IE, the file upload box will show the full path (eg C:\.....\file.txt) where Firefox and Chrome just show the filename (eg file.txt)

So for working with most browsers, my code above has that parsing array thing.
reply
    Bookmark Topic Watch Topic
  • New Topic