permaculture playing cards*
The moose likes Struts and the fly likes What to do if you must receive a File then return a File array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "What to do if you must receive a File then return a File array" Watch "What to do if you must receive a File then return a File array" New topic
Author

What to do if you must receive a File then return a File array

Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Good day!

I have a JSP page which has an input field with "browse" which when clicked by the user he will choose a file. After that my program will get all the files inside the same directory where the first file (which was previously chosen)is located.

As mentioned earlier I have a JSP page, what I did was I constructed a class which will traverse all the file in that directory. In the class I declared



thinking that from the JSP page, I am expecting to receive a File. However, after traversing through the files I need to populate the filenames or file paths in a table in the same JSP page. So what I did was I declared



in the Action Class. My plan is to convert a File[] or File array into a List so that after which I will populate it and send it back to the JSP page. My class that suppose to traverse through the files puts the files in a File array.

My problem is that in my traverse class, which has a "main" method, I am receiving a File to be able to get the other contents of the same directory but in my Action Class, I am expecting a File array to be returned by the traverse class. So that I can convert it to a List and then populate it. But I can't do it because my traverse class doesn't return a File array.

Do I need to remove the "main" declaration and change it to a method? Or should I do something else? Hopefully, someone can give me an advice.
Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Darren Alexandria:
I have a JSP page which has an input field with "browse" which when clicked by the user he will choose a file. After that my program will get all the files inside the same directory where the first file (which was previously chosen)is located.

Sorry, but a server-side java program such as a Struts Action class simply cannot do that. The <input type=file> tag (or the <html:file> tag) in a JSP can allow a user to browse their local directory and pick one file to upload, which is sent in the form of a binary data stream. Because the Java program resides on the server, it has no visibility at all to the client's file system. It can accept what the client sends it but has no way of browsing the client's directories or looking at its files.

Think about the massive security nightmare that would be if this were possible. Can you imagine every website you ever visited having full access to all the files on your computer?

If you want to write a program that can do the things you've outlined, you must use some sort of client-side technology such as a Java Signed Applet, Flash, or JavaScript.


Merrill
Consultant, Sima Solutions
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Wow, THANK YOU for the advice.

Now I am enlightened.
I am doing this thing for almost 3 months only to find out that
it won't work using that way. Now I will start a new one again using
applets. No regrets. I have learned a lot of things. =)

Again, thank you so much!
God bless you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What to do if you must receive a File then return a File array
 
Similar Threads
list() method - File Navigation
File's list() method and classpaths
optionsCollection syntax trouble
Ajax: how to achieve
Cant Redirect struts2.0