• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to save files outside the webapp folder?

 
Lorana Berd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lorana Berd
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you a million.

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

Thanks,
Lorana
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure!
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic