This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes How to save files outside the webapp folder? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to save files outside the webapp folder?" Watch "How to save files outside the webapp folder?" New topic
Author

How to save files outside the webapp folder?

Lorana Berd
Greenhorn

Joined: May 05, 2008
Posts: 7
Hi,

Could you please help me with hte following

I need to save files in my web app and I need to save them outside the web application folder so I don't lose them during new deployments.
Now I am saving them inside getServletContext( ).getRealPath("/") + "data" folder and they end up inside approot/data folder.
How I can point to some other folder on disc not related to the app server.

Is there solution which would work for both Windows and Linux, since my development is Windows and production is Linux.

Thank you very much,
Lorana
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Well, obviously you won't be using getRealPath().

If the folder location will differ between where the app is deployed, you could specify the path as a context param in the deployment descriptor and set is as appropriate for each deployment. If you'd rather not mess with having to change web.xml every time (I know I don't), you could put this type of information in an external properties file that the web app reads upon startup (a context listener is great for this) and stores in application context.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lorana Berd
Greenhorn

Joined: May 05, 2008
Posts: 7
Thank you so much, it works perfectly !!!

I would just like to ask you one more question (style wise).
Should I put some dev or production parameter too in the properties file and in application I ask
if it is production to go to certain folder or if it is developemnt to go to diffferent folder?
I have never knew the best way to do these kind of things (different parameters for production, staging and development) nicely.

Thank you,
Lorana
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

That's the beauty of having an external properties file that is not part of the web application. You can have one file on the dev system that specifies one folder, and a file on the target production system that defines another.
Lorana Berd
Greenhorn

Joined: May 05, 2008
Posts: 7
I am sorry to bother you more, but I would like to clarify this once for all and it seems that you can
hep me.
When you say external property file is that file part of the build (somewhere in webapps/myapp/some_config_folder ) or it is outside of myapp folder.
If it is outside myapp folder where is the best place for it to be?

Thank you so much,
Lorana

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Lorana Berd wrote:I am sorry to bother you more

Don't be. That's what these forums are for!

If it is outside myapp folder where is the best place for it to be?

Depends how you are obtaining the properties. If you are letting the class-loader do the work, then anywhere in the classpath is fine. For Tomcat 6, you could drop the properties file in the lib folder (common/lib for Tomcat 5).

Lorana Berd
Greenhorn

Joined: May 05, 2008
Posts: 7
Thank you a million.

Finally I uderstand how to do this now.
My lucky day today

Thanks,
Lorana
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

My pleasure!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Lorana Berd wrote:Is there solution which would work for both Windows and Linux, since my development is Windows and production is Linux.
It´s useful to know that paths like "/usr/appserver/files" works on both environments. In Windows it actually points to the desired directory at the same disk as where the server runs. If it is for example C:\, then it actually points to C:\usr\appserver\files.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to save files outside the webapp folder?