Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

to save a file dynamically

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

my requirement is to save the file dynamically into the local system of the user who ever runs the servlet. iam able to do it with in my local environment with the following code


String p = System.getProperty("user.home");
String C = p.substring(0,3);
File outputFile = new File(C+"Coo_"+ strDate_MMDDYY+".xls");
FileOutputStream out = new FileOutputStream(outputFile);


but its not working when i promote the code to Test environment and try as user please can any one let me know the defect in my code or Any other approach
Thanks in Advance
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all! You are not trying to save a file in the user home directory!
You are requesting the home directory and trying to parse out the drive where the home directory is located.


Second: My question to you is: Are you trying to save a file on the filesystem of the server? Because this is what you are doing! You are asking the home directory of the user who is running the server. In a servlet you are not able to write files on the client's filesystem and I think that this is what you want to do.

Of course this works in your development environment because the server and client are located on the same system.

Tell me if I am wrong! Maybe you should explain a little more about what you are trying to do exactly and then we can help you more!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...but its not working...

What does "it's not working" mean - do you get an exception, or does it appear to do nothing at all?

What is the value of the system property "user.home" on the test system? Add some log statements (maybe just simple System.out.println(...) statements) to your code to find out what's happening when you run it on the test system.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,


To write to file dynamically in server, we can do following,

1) From your mail it seems that you are running server on Windows machine, thats why you are tring to get the drive ( first 3 chars).

Ok... Take that then, create a FileWriter and write everything into that one.

Assume you have a etc folder in c:\ Drive into which you want write the data ( on Server ) into a file hello.txt ... then do following....


System.out.println(System.getProperty("user.home"));
String kk=System.getProperty("user.home");
String k=kk.substring(0,3);
FileWriter fileWriter=new FileWriter(k+"etc\\Hello.txt");
String kkk="Hello";
fileWriter.write(kkk);
fileWriter.close();

It will serve your purpose. you can pass true parameter to append

More inputs welcome ....
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic