5

I have a web application with an upload form where users can upload files.

I'd like to store the files a folder 'files'. And i'd like this folder to be placed directly under the webapp-root.

Using struts2, in my uploadFileAction, i can easily set this path with String uploadDir = ServletActionContext.getServletContext().getRealPath("/files")+ "/";

But when i'm trying to retrieve the files, apparently my bean which tries to access the file does not have access to getServletContext() and thus throws a nullpointer on getRealPath

Now im trying to figure out how i should approach this. I've heard about spring resource is the way to go, but i cant find any relevant examples.

Is there an easy way to get my paths? I just want the rootpath to my webapp. Nothing more nothing less...

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
user829237
  • 1,579
  • 8
  • 35
  • 57

2 Answers2

8

I'd like this folder to be placed directly under the webapp-root.

This isn't going to work. All those files will get lost whenever you redeploy the webapp or even when you restart the server. Those files are namely not contained as part of the original WAR.

You need to store them in a fixed path outside the webapp's context. E.g. /var/webapp/uploads. You can configure this path in a properties file or as a system property.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Wow. I didnt think about that. Thanks for pointing out my obvious error. But as im moving between platforms (linux-windows) i need a relative path to my upload dir. I dont like to edit a properties file every time i make a platform-jump... – user829237 Sep 01 '11 at 18:11
  • 1
    In Java `File` API, Unix-like paths like `/var/webapp/uploads` work perfectly fine in Windows as well. It'll just be relative to the disk where the code is executed from and Java will take care about slashes. E.g. when server runs on `C:`, it'll end up as `C:\var\webapp\uploads`. – BalusC Sep 01 '11 at 18:14
  • Thanks for clearing that bit up. Forgive me for being a unix-newb, but im still a bit unsure as to how to specifiy the path, since my unix server is a shared host and i think i only have access to the ~ directory and i'll need to put the files somewhere under that one.. Any pointers would be of great help! – user829237 Sep 01 '11 at 18:22
  • You could just create a folder? Note that this needs to have r/w access. – BalusC Sep 01 '11 at 18:24
  • 1
    I meant, how to specify the real path to such a directory as ~. But i think i figured it out. Its real path was /home/*username*/ – user829237 Sep 01 '11 at 18:27
  • Oh, inside the files folder, use `pwd` (print working directory) command to figure full path. – BalusC Sep 01 '11 at 18:32
  • Thank you very much BalusC, you have been of great help! – user829237 Sep 01 '11 at 18:37
3

I've found that a few ways to accomplish this regardless of what OS platform you're using. I typically like to store my files in some way relative to my web application. This being said, the easiest way to do this is using your class file as a reference to get the real path. I use something similar to the following to store image uploads for a few web applications that I've built:

public class FileLocationTest {

    public static void main(String[] args) {
        String path = FileLocationTest.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        System.out.println(path);
    }
}

This can be done using a static utility class in your web app, or any class for that matter to obtain the real path of your application regardless of OS. Alternatively in a servlet I've also used the request class to get the Tomcat location if my appbase is in a different area, something like this:

    String location = request.getClass().getProtectionDomain().getClassLoader().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
    String path = location.substring(0, location.indexOf("/tomcat")) + "/data/events/images/";
Russell Shingleton
  • 3,096
  • 1
  • 20
  • 29