Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing a new file to the context root

 
raki sugi
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
 
Tim Moores
Bartender
Posts: 2895
46
  • 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?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java 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
Bartender
Posts: 2895
46
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic