Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

write File to server with servlet

 
Lukas Benberg
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it! thank you
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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") .
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doesnt the war explode when you deploy it?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic