• Post Reply Bookmark Topic Watch Topic
  • New Topic

write file dynamicly

 
Sebastian Green
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a file on the webserver then make it downable. To make the txt file I have to define the whole path like C:\tomcat\webapps\mywebapp\file.txt. Thats works fine ONLY if tomcat is installed on that location. If I move mywebapp tp an other tomcat server (colud be located F:\tomcat) it wont work

Is there a dynamic solutions for this?
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sebastian Green:
To make the txt file I have to define the whole path like C:\tomcat\webapps\mywebapp\file.txt.


Any particular reason why? Files, FileOutputStreams and FileWriters can take relative paths in Java, so you could make the file path relative to your servlet's home directory. You could also make use of File's static createTempFile() methods to utilize the platform-specific temp directory.
 
Sebastian Green
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have no reason why I cant create files relative. Simply I havent been able to do it. Pls tell how.

My workaround for the problem was the System.getProperty("user.dir") and worked my way out of there.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian,

A neater solution for a web app, if appropriate for your requirements of course, is to never write any files. That way you don't have to worry about where they are written to which is nice and stress free. The way I do this is to send the data to the servlet output stream returned by response.getWriter(). If you set the response type to something that the browser doesn't recognise it will force a download dialog.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!