• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

write file dynamicly

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Grow your own food... or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic