wood burning stoves 2.0*
The moose likes Tomcat and the fly likes writing a new file to the context root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "writing a new file to the context root" Watch "writing a new file to the context root" New topic
Author

writing a new file to the context root

raki sugi
Greenhorn

Joined: Jul 22, 2011
Posts: 2
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jul 22, 2011
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Yes, a very simple one: web apps have no concept of a "default directory", so any relative path is basically undefined.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing a new file to the context root
 
Similar Threads
EE application architecture
How to create XML file at runtime in Projects root folder - context root in Struts1.2 or 1.3 ?
Enable Root Context in Tomcat 5.5
Servlets not found in ROOT dir - OK, in examples????
Need to assign a context root for my application