1

The following code returns the path to the war folder (i.e. /ROOT/):

getServletContext().getRealPath("/");

I need to go one folder up from this point, how do I go about doing that?

I have tried the following:

    final String path = getServletContext().getRealPath("./");
    final String path2 = getServletContext().getRealPath("../");
    final String path3 = getServletContext().getRealPath(".../");

I need so save files outside of the ROOT folder, but only one level up, so that everytime I update my website, it doesn't replace the physically uploaded files within the ROOT folder, and rather only touch the web site files.

Thanks

TejjD
  • 2,571
  • 1
  • 17
  • 37
  • 1
    You want to save files into Tomcat's `webapps` directory? Do you think that's really a good idea? – Kayaman Dec 22 '15 at 18:56
  • It will not be saved there, it will be saved within the domain folder.. my folder structure is "domains//folder_i_want_to_save_to" – TejjD Dec 22 '15 at 18:57
  • 2
    Sounds like a bad idea in any case. – Kayaman Dec 22 '15 at 18:58
  • Can you assist me with a answer? The main issue is the web hosting site I am using is useless and is not providing me with the public ftp url to any folders I create. So my only option is to temporarily store the items one folder up. Thanks – TejjD Dec 22 '15 at 19:00
  • Ugh, so this is really about fighting a lousy provider... – Kayaman Dec 22 '15 at 19:07
  • Yes essentially. The site allows for clients to upload documents, and the documents are being stored within a folder that is currently inside the ROOT folder. And now what is happening is everytime there is a site update, the ROOT folder is deployed, and the files are replaced. Hence why I need to store them elsewhere. – TejjD Dec 22 '15 at 19:09
  • Never, *never*, NEVER use `getRealPath()`. Any attempt to use it indicates a bad practice solution. If you can't use the local disk filesystem, use the database. If that's even not offered by your hosting, well, look for a better one. E.g. OpenShift and it's even completely free. – BalusC Dec 23 '15 at 12:56

3 Answers3

0

We don't even know if your WAR files are unpacked: Tomcat can well deploy them without unzipping, which leaves you with a non-filesystem-path.

As discussed in the comments, it's bad practice to do this, you should rather figure out where to store the data - for example: database, external storage, somewhere in the file system. Then "just" provide a separate download option for those files - individually or zipped - through your web application.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
0


Another solution you can use is a symbolic link to an external folder. You can create a folder out side your project let's say for the example at the same level of the webapps folder in your tomcat path. We will name uploads for the example.

Now in your deploy script (assuming you have one), you can easily create symbolic link from the uploads folder to a folder inside your project. It will allow you to get the files from inside of your project, and when you re-deploy, the files are not delete.

For example:

ln -s /var/lib/tomcat6/uploads /var/lib/tomcat6/webapps/myWebApp/uploads

The example above is for my distro of tomcat6 but you can change the path to work with your setup.

This answer uses 3 assumptions:

  1. Your WAR file is unpacked.
  2. You have a deployment script and if not you can easily create a bash script to do so.
  3. The solution you need is for a non distributed setup (each server holds its own files).

Liron

Liron
  • 515
  • 4
  • 20
-1

You have to use the File's API over war's location.

   File root = new File(getServletContext().getRealPath("./"));
   String parent = root.getParent();

   String parentParent = new File(parent).getParent();
Ansemo Abadía
  • 488
  • 3
  • 10