• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
harry flower
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but my concern is which directory in the web server should I temporary stored these files?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any place that's not publicly accessible, e.g. a subdirectory of WEB-INF.
 
harry flower
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic