views:

36

answers:

1

How do I upload a file from a Java servlet to a location on a web server within Tomcat/webapps.

I am using Commons upload. I have a location such as myserver:8080/myapp/mylocation where I want to put the files that are uploaded.

I tried using getServletContext().getRealPath("/"); to find where I am and then appended that with mylocation but I get a nullpointer exception.

I know I sound vague, it's because I am confused about the big picture, what are the generals steps I need to perform to make this work. Any code or links to code would be much appreciated.

+1  A: 

You may need to check permissions. Make sure that you have write permissions on the directory in which you plan to store the file. Generally you may want to store the file in WEB-INF/mylocation. Then getServletContext().getRealPath("/WEB-INF/mylocation") becomes the location of where you are going to save the file.

Vincent Ramdhanie