• 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

Writing to folders outside of a WAR file

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

This is the first time i have used a war file to deploy an application. In the past due to server hosting restrictions i have just copied the directories over.
I am able to create my war file fine through eclipse. From what I have read each time i upload a new war file it will overwrite EVERYTHING from the old one, including all the files and folders when it explodes.
However there is one folder that my application needs to write to. It will be a folder that i upload pictures to. How/Where do I place this folder so it does not get overwritten when i update the war file in the future.
Right now i upload ROOT.war into my webapps folder and the project deploys fine. However the uploadedImages folder always gets overwriten.

TIA
John
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to create a directory outside of the web app directory hierarchy.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:You need to create a directory outside of the web app directory hierarchy.



Ok, then what would my path look like to get to that folder?

Say i create an uploadedImages folder in my webapps folder where the root folder is
now my application is in the root folder and the images will go in the uploadedimages folder.

webapps
-root
-uploadedimages
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Schretz wrote:

Ulf Dittmer wrote:You need to create a directory outside of the web app directory hierarchy.



Ok, then what would my path look like to get to that folder?



It would look exactly like what it looked like when you created it. Perhaps I shouldn't belabour the obvious, but if the path for the directory was "/users/web/data" when you created it then it would be "/users/web/data" when you wanted to use it.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry what i meant was currently i use:

String uploadFolder = getContext().getRealPath("/uploadedImages");

So you are saying instead I should use:
String uploadFolder = "webapps/uploadedImages";

Or do i use:
String uploadFolder = getContext().getRealPath("./uploadedImages");

sorry if i sound stupid i just want to do it the right way and not hack it
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Schretz wrote:So you are saying instead I should use:
String uploadFolder = "webapps/uploadedImages";



What we are saying is to use the path to the folder. Obviously you can't use getContext() to help with this because the folder you're going to create is not within the application's context. Just use the path to the folder. And don't use a relative path because you don't know what Tomcat's current working directory is. Use the full path to the folder.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of my favorite rant topics and now you know why. I had a massive number of uploaded image files get nuked because some idiot wrote the app to upload into the WAR.

On a Linux system, the coventional place to upload files for long-term storage would be under the /var directory. Most commonly, someplace like /var/lib/appname, where "appname" is the name of your application.

However, I don't recommend hard-coding this path into the webapp. What I normally do is feed it in as a webapp context variable. If I define the directory path in a Tomcat Context, I can use JNDI to retrieve it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic