File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Working with files... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Working with files..." Watch "Working with files..." New topic

Working with files...

George Larry
Ranch Hand

Joined: Nov 07, 2001
Posts: 52
I'm experiencing a small problem...
I created a page that takes text from a file on my machine to play with... This worked great while I was running tomcat on my machine, but I tried to move it to the server and now it can't locate the (local) file... I suspect that, even though I'm browsing on my machine to locate the file, once I give it the path... it (the server) is trying to find the file on itself (the server).
Is that what is happening?
If so... How do I work around that? Do I have to upload all the files to the server before I can access them or is there some way to retrieve files data from the users machines?
Any ideas on what to do and how to do it would be appreciated.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
All of the code you put into a JSP is translated into a servlet and is executed server side - the resulting HTML is sent back to the client. So, your page is looking on its local drive for the file in question.
There is no way to access a client side file unless you upload it to the server first. You might be able to do something through an applet if the clients security setings are set low enough.

Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
Hi, George,
you could use a HTML-form to browse any file on your local drive and submit it to a Servlet using multipart/form-data. There won't be any security issues since you use plain http.
The HTML-File could look like this:

Adjust the URL to a Servlet of your choice. This has to be able to decode the POST-Data that has been sent. The following method can do this:

Adjust this method to your needs. Call it in the doPost() of the Servlet like this:
String[] lines = readInput(request);
As you see it returns a String[] containing every line of the original document.
This is an example. You could do this with binary files as well. You don't need to save the files to the server - you can keep them in memory and make use of it.
I agree. Here's the link:
subject: Working with files...
It's not a secret anymore!