aspose file tools*
The moose likes Tomcat and the fly likes Writing to folders outside of a WAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Writing to folders outside of a WAR file" Watch "Writing to folders outside of a WAR file" New topic
Author

Writing to folders outside of a WAR file

John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
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
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
You need to create a directory outside of the web app directory hierarchy.


Ping & DNS - my free Android networking tools app
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188
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
Bartender

Joined: Oct 14, 2005
Posts: 18672
    
    8

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

Joined: Sep 10, 2008
Posts: 188
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
Bartender

Joined: Oct 14, 2005
Posts: 18672
    
    8

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

Joined: Jun 25, 2001
Posts: 16145
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to folders outside of a WAR file