wood burning stoves 2.0*
The moose likes Servlets and the fly likes Accessing Files from a Web App Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing Files from a Web App" Watch "Accessing Files from a Web App" New topic
Author

Accessing Files from a Web App

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I have a rather odd requirement for a web application that I'm working on. I need to read a file (from within the web application) and then create a new file (either within or outside the web application). For example, let's say I have a file called input.txt in my webapp directory. I need to be able to read that file (from a servlet or some other Java class) and then I need to write a new file to some other location, such as C:\dest.

I have some code in my servlet that looks like this:



I'm guessing, from this, that there are some security measures in place to prevent you from reading files within a web application. Anyone happen to know what those are? I'm by no means a web app guru so this stuff is all a bit new to me. Any ideas how I might get at that file to read it?

Will I have similar problems trying to write a file to the local file system from my web app?

Thanks,
Corey
[ November 10, 2004: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
As the application is a web app thus i am assuming that the file would be received from the client. if yes then i think there is file uploading involve before actual reading and writing file.

I am not reaching towards any security issue, file uploading is going to be handled by the doPost() method of your Http. i think the security issues and constraints would be the same as we have in simple form posting.

But would like to suggest something that never ever handle DB or kinda file I/O stuff inside your servlet. just use the servlet as a controller of your request/response mechanism. do these kind of I/O stuff in some POJO.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
I have tried something like the following in my web app successfully,

- read blob from db
- convert it into an image file
- show it to the user

I think you would be able to read the file as well, dont think there is some security issue that prevent from reading the file. it sounds like a normal kind of reading. isn't it. we have a file in our own machine, we have the rights as well, we know the path, so why we cant read it?
[ November 10, 2004: Message edited by: adeel ansari ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by adeel ansari:
As the application is a web app thus i am assuming that the file would be received from the client.


Actually, that's not the case. All of the file processing is going to happen on the backend and the user will never see it happen. A form is submitted, which gives me some information about what to process, I process the files, and then redirect the user to some other page.

The file processing won't actually be done in the servlet, but I was trying to get the basics working last night when I ran into this issue.

Any more ideas?
Larry Cryderm
Greenhorn

Joined: Mar 23, 2004
Posts: 9
I can't see any reason your read should not work. I read and write XML files into my servlets all the time. Could it be permissions on the file? Can you open the file in any other app?


I shoulda been a rock star.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Larry Cryderm:
I can't see any reason your read should not work. I read and write XML files into my servlets all the time. Could it be permissions on the file? Can you open the file in any other app?


Is it, by chance, the path I'm using to access the file? I was trying to use the absolute path (C:\...). Would it be possible to use a relative path, such as \mywebapp\myFile? Would that make any difference?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

I was going to suspect file permission problems, but then I saw you were doing this on Windows which doesn't have much of that.

Yet... Is your container running as a service? Could it be that the container is running as one user, but the file is 'owned' by another?

There's no implicit security constraint on web apps accessing files outside of the app context, so something else must be at work here.
[ November 10, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Larry Cryderm
Greenhorn

Joined: Mar 23, 2004
Posts: 9
another idea....the path to the file, did you use double "\" ? Like:
String myFile = "C:\\myDir1\\myDir2\\myFile.txt";
.....that'll bite you.
also, off the File object show the file.getAbsolutePath() .....so you can confirm you're pointing to where you want.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Larry Cryderm:
another idea....the path to the file, did you use double "\" ? Like:
String myFile = "C:\\myDir1\\myDir2\\myFile.txt";
.....that'll bite you.
also, off the File object show the file.getAbsolutePath() .....so you can confirm you're pointing to where you want.


Well, I checked the results of .exists() on the File object and that returned true so I'm assuming I'm pointing at the correct file. I can see that the file is there, but I can't open it.
Napa Sreedhar
Ranch Hand

Joined: Jan 29, 2002
Posts: 62
C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\mywebapp (Access is denied)

check 1) Are you using a file or a directory?
check 2) Did you try putting the file in someother place i.e. no spaces in the file path e.g. C:\Examples\hello\hello.txt

I see know problem doing the process but curious to know what you are doing by reading a file and writing to a differnt file using a servlet?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Bah. As usual, the problem was entirely my fault. My paths, which I was certain were correct, weren't.

Sorry for the trouble. It's working like a champ, now.
 
Don't get me started about those stupid light bulbs.
 
subject: Accessing Files from a Web App
 
Similar Threads
How to read a txt file with space
Reading a text file
Printing from a file
FileNotFoundException & Try/Catch
Servlets and relative file path io