2

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
jack
  • 1,902
  • 5
  • 32
  • 43

1 Answers1

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
  • 2
    Note 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