aspose file tools*
The moose likes Servlets and the fly likes Where to save a file separately for different users in same web server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Where to save a file separately for different users in same web server?" Watch "Where to save a file separately for different users in same web server?" New topic
Author

Where to save a file separately for different users in same web server?

harry flower
Greenhorn

Joined: Aug 29, 2007
Posts: 19
Hi,

I am writing a web application to be run in Unix Server.
I wish to save a file from a FTP server to the web server.
But this file is to be save temporary in the web server, make same updates and will be deleted after used.
As there might be many users downloading the same file but the
content of this file might be change differently depending on different users, so usually where is the best location in the web server to store this file separately for different users?

Thanks in advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
Could you change the filename so that it contains the userID or user name? That way the files wouldn't get in one another's way.


Ping & DNS - my free Android networking tools app
harry flower
Greenhorn

Joined: Aug 29, 2007
Posts: 19
Yes but my concern is which directory in the web server should I temporary stored these files?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
Any place that's not publicly accessible, e.g. a subdirectory of WEB-INF.
harry flower
Greenhorn

Joined: Aug 29, 2007
Posts: 19
Then is there a java command to access the subdirectories in WEB-INF?
I tried to use this command: 'getServletContext().getRealPath("/")',
is it pointing to the correct location?
I am running my web application in Eclipse.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by harry flower:
Then is there a java command to access the subdirectories in WEB-INF?
I tried to use this command: 'getServletContext().getRealPath("/")',
is it pointing to the correct location?
I am running my web application in Eclipse.


Hi Harry,

From you java web application code you can access the files present within your WEB-INF directory , but it would not be visible to the external world.

I do not think that there is a way to run web application in eclipse but you can use tomcat plugin for eclipse and use that for general tasks.But this again would connect to a Tomcat instance only.

You might want to give the user a download option so that the use can download the file specific to his id in his local workstation.

Hope this helps,
[ September 12, 2007: Message edited by: Rahul Bhattacharjee ]

Rahul Bhattacharjee
LinkedIn - Blog
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by harry flower:
Then is there a java command to access the subdirectories in WEB-INF?
I tried to use this command: 'getServletContext().getRealPath("/")',
is it pointing to the correct location?
I am running my web application in Eclipse.


Your Java code can access files under WEB-INF.
That directory is only hidden from direct access to the web.

If your app is pulling this from an FTP server, then you should put it in the tmpdir provided by your container.

From the servlet spec:

SRV.3.7.1 Temporary Working Directories
A temporary storage directory is required for each servlet context. Servlet containers must provide a private temporary directory for each servlet context, and make it available via the javax.servlet.context.tempdir context attribute. The objects associated with the attribute must be of type java.io.File. The requirement recognizes a common convenience provided in many servlet engine implementations. The container is not required to maintain the contents of the temporary directory when the servlet container restarts, but is required to ensure that the contents of the temporary directory of one servlet context is not visible to the servlet contexts of other Web applications running on the servlet container.


Then you can either rename it to contain the user's ID (as Ulf mentioned) or place it in a subdirectory with the user's id in the name to insure that it is separate from other user's copies.
When it's time to deliver it, use a servlet that streams files from the web server to the browser.
We have an example application that streams file in our codebarn.
http://www.javaranch.com/codebarn/codebarn-servlets-simplestream.jsp

One thing that you'll need to modify is how the file is read.
In our example, we're streaming files that were shipped with the app and are located under WEB-INF using ServletContext.getResourceAsStream.
In your case you'll want to use a fileReader which will allow you to work with an instance of java.io.File.

Lastly, tempdir doesn't necessarily (as it's name implies) mean that the container will clean up the contents. You will want to make sure you delete this file at some point so the size of your tempdir don't grow out of hand.


The big advantage to this is that it is a truly portable solution.
Java web applications (unlike PHP or ASP apps) aren't necessarily always deployed as directory structures on a file system. They are often deployed as war files (which are sometimes packed further in ear files). When they are deployed this way, you can't write files to WEB-INF or any other part of the web app's directory structure because one doesn't exist. Doing it this way will insure that your app will always have a real directory to which it can write this file.
[ September 13, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where to save a file separately for different users in same web server?