jQuery in Action, 2nd edition*
The moose likes JSF and the fly likes get directory name from the local file system Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "get directory name from the local file system" Watch "get directory name from the local file system" New topic
Author

get directory name from the local file system

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
I am trying to get the name of the local file directory name using web interface and when the user selects one directory name, have to write the name into a database.

1. Is there any ChooseDirectoryName as FileChooser in Java?
2. Under this situation, JSF or JSP is useful?

Regards and Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15661
    
  15

A web server is not a file server. So being able to find the name of a directory on the web server is pretty meaningless for a web client - there's no way for the web client to access that directory or its files directly unless file-serving software is also installed on the web server machine, the client has an appropriate file-server client software (filesystem driver) and the necessary network and user security privileges.

So no, there's little utility in being able to get the server directory pathname in a JSF or JSP page. Also, when the "directory" is actually part of a WAR, it may not exist as a physical disk directory item at all.

In general Java, there are several methods of getting a directory's path. The most common way is with the java.io.File.getAbsolutePath method. For example:

String pathString = new File(".").getAbsolutePath();

will set pathString to the filesystem path of the current working directory (".").


Customer surveys are for companies who didn't pay proper attention to begin with.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15661
    
  15

Hmmm. I think I misread that - it sounds like you want to get a directory name from the client's file system and send it to the server.

The primary issue is figuring out what "the" name should be. Probably the working directory of the web browser, but that's generally not likely to be useful to the server.

Without a signed applet or similar construct, the browser can't reach outside itself to get filesystem info. The closest it can get is when you use the HTML file upload facility, which means actually sending a file, and I don't recall if the meta-data for the upload includes the client's directory path or not. If it does, you can take it and do a "new File(uploadname).getParent.getAbsolutePath();"
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Both of Tim's guesses seem pretty useless to me. The client can't do anything useful with the name of a directory on the server, and the server can't do anything useful with the name of a directory on the client.

Could you clarify your question? Was it either of those two? If so, why? Or if not, what did it mean? (Your word "local" isn't very precise... "local" to what?)
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks Tim and Paul for your responses. Let me be more clear in explaining my objective.

For example if I have the following directory structure in my PC as C:\dirNames\test1, test2, .. testN

In my browser, I have to allow the user to see the list of files under as a drop down list which includes test1, test2... testN. And when the user selects any one item in the list, I want to write that value into an sql database. Along with this values, I will be collecting some more details from the user in the same page.

But my main goal would be in displaying the list from a particular path. Is it possible to do with JSF? or in JSP? Please let me know.

Regards
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15661
    
  15

No, it's not possible in JSF or JSP, because they do their work on the web server and the web server cannot access your local file system.

As I mentioned in my second post, your options in accessing local system resources from a web browser are limited, for security reasons - the so-called "sandbox".

In order for a Java solution to work, you'd need a Java applet, which is code running inside the browser system on the client computer. However, the applet sandbox rules expressly prohibit access to the client computer's filesystem, so you'd have to create a signed applet. Then you have to have someone manually permit the applet to run, which last time I looked was a cumbersome process that casual users wouldn't be able to manage.

What about other client-side languages? As I understand it, JavaScript can access the local filesystem, but it, too requires that the scripts be digitally signed.

In fact, no browser-contained application system should be able to read or write files or otherwise gain access to the local directory tree without strict authorization for the very simple reason that that is exactly the mechanism that facilitates the installation of malware and causes data leaks. Unfortunately, the bad guys have made life difficult for legitimate applications.

There is, by the way, another reason why this sort of interaction isn't better supported. In an enterprise environment, user computers are just about the last place you want to store critical business assets. They frequently don't get backed up, the data cannot be easily shared (depnds on how you define "easy", though), data access control is potentially poor, and, finally, in the case of laptops, you have the risks of loss, theft or damage of the computer and the data in it.

I think, however, that the real question is "what specific benefit do you get from knowing what's in the user's filesystem"? Since you're not actually transferring the files, would it be possible to simply create a "virtual file tree" stored in the database or somewhere that the server can access?
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
As a beginner I tried something like below. I just need the names of all the directories under C:\testing and have to display thelist for the user and when the user selcts one directory, I have to pick that value and store in the database. Righnow I am juts trying to display the value in another page and database would be my next step.

In my JavaBeans.java class apart from many beans, I have added this method below to get a list of directory names as alist test.
I have to include exception part in future.


While trying to display this list of directories in my jsf page I was successful in the initial setup as below.
Displaying the list inside a input box.

I could see all the directory names separated by comma.

But when I tried listing as drop down menu as below, I get java.lang.ClassCastException: java.lang.String



How do I solve this? Thanks.
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Yes, I could managed to do it by changing
into


and it works now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get directory name from the local file system
 
Similar Threads
IgnoreCase
problem finding resouircebundle?
How to get log4j directory path
adding jar files
JFileChooser save dialog without filename