This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes write File to server with servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "write File to server with servlet" Watch "write File to server with servlet" New topic
Author

write File to server with servlet

Lukas Benberg
Ranch Hand

Joined: Jan 03, 2007
Posts: 45
hello,
I would like to write a File to the server with a servlet. I tried it with File dest = new File("test.xml"); this does not seem to work. do I have to find out the absolute path on the server? if yes, how? my servlet is running in a tomcat.

thank you!
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42955
    
  73
Yes, you do need to use absolute paths, because there is no notion of a "default" directory in a web app. You can find out the absolute path to the WEB-INF directory via
Lukas Benberg
Ranch Hand

Joined: Jan 03, 2007
Posts: 45
got it! thank you
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Lukas Benberg:
hello,
I would like to write a File to the server with a servlet. I tried it with File dest = new File("test.xml"); this does not seem to work. do I have to find out the absolute path on the server? if yes, how? my servlet is running in a tomcat.

thank you!


I would have written the file in the temp directory of tomcat.You can get the location of the temp directory using System.getProperty("java.io.tmpdir") .


Rahul Bhattacharjee
LinkedIn - Blog
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

One thing to keep in mind; getRealPath will only work if your application is being run from an exploded file system. If, on the other hand, your application is being run from a packed war archive, getRealPath will return null.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Gobind Singh
Ranch Hand

Joined: Aug 04, 2006
Posts: 62
doesnt the war explode when you deploy it?
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42955
    
  73
doesnt the war explode when you deploy it?

Not necessarily. E.g., Tomcat uses the unpackWARs attribute of the Host element in the conf/server.xml file to determine whether or not to explode war files.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: write File to server with servlet