Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

writing a new file to the context root

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am debugging some old code and it has a File upload mechanism. The uploaded file is temporarily stored in the server. But the problem I am facing is, the file gets written to Tomcat_Home/bin folder. I want to change this and write to the context root. How do I do this?

Thanks,
Rakesh
 
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code probably uses relative paths; you should never do that in a web app. Use an absolute path that points to a location within your web app, or -even better- a location outside of the Tomcat directory.
 
raki sugi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim. Yes, it does use relative path to create a file.

But is there any reason why relative paths in webapps are not recommended?
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you never know what the current directory is that they will be relative to. That's why you should always use absolute paths. To turn a relative (to the web application) path into an absolute path you should use ServletContext.getRealPath.
 
Tim Moores
Saloon Keeper
Posts: 5710
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a very simple one: web apps have no concept of a "default directory", so any relative path is basically undefined.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!