• Post Reply Bookmark Topic Watch Topic
  • New Topic

to save a file dynamically  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • 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 ....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!