• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to folders outside of a WAR file

 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Paul Clapham
Sheriff
Posts: 21155
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 21155
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18226
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic