I deployed my webapp in Tomcat, but when I prefix the filename with \ I end up in the root of my server (C) folder, and if I don't prefix it with \ I end up in the bin folder.
The former I expected, the latter I didn't.
What's the cleanest way to write to a folder in my webapp(userControlWebApp\images) because just doing ..\webapps\ + request.getContextPath()
doesn't seem the cleanest way.
Asked
Active
Viewed 3,538 times
2
-
Can't you use an absolute path? – Shervin Asgari Jan 06 '11 at 12:34
-
That's well so absolute... I like to keep it a bit dynamic. – jack Jan 06 '11 at 12:36
1 Answers
8
Use getServletContext().getRealPath(..)

Bozho
- 588,226
- 146
- 1,060
- 1,140
-
Can't believe I forgot that one. Just tried System.getProperty("user.dir") and that also seems to work. Any reason to use one over the other? – jack Jan 06 '11 at 12:37
-
prefer the API - getRealPath. I'm not sure if the other is guaranteed to work always. – Bozho Jan 06 '11 at 12:38
-
2Note that the folder and all its contents will get lost whenever you redeploy the webapp. It's not a good choice if you want to make it permanent. See also this related question http://stackoverflow.com/questions/4548190/best-location-for-uploading-file/4548320#4548320 – BalusC Jan 06 '11 at 16:13