• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Servlets: writing file to server

My applet would upload a string to a servlet. I know that the string is received by the servlet correctly. I want to know how can I write this string to a file that is not exist before.

I have tried to use the code like below, to write the string "Hello World" to the file "/home/agnesko/hello.txt". This file is not exist in the file system before. However, it does not work!

 int read;
 String text = "Hello World";  
 try {
  FileOutputStream out = new FileOutputStream("/home/agnesko/hello.txt");
  out.write(text.getBytes());
  out.close();
 }
 catch(Exception e) {
  e.printStackTrace();
 }

anyone have idea?
0
agnesko
Asked:
agnesko
  • 5
  • 3
  • 3
  • +2
1 Solution
 
heyhey_Commented:
you can WRITE (neither you can READ) file to remote machine - you need some kind of server support. generally speaking web servers allow reading files from remote computer and ftp servers allow reading & writing.

File object ALWAYS use local file system.

now - please - formulate your question again.
0
 
Laminamia063099Commented:
int read;
                 String text = "Hello World";  
                 try {
                  PrintWriter out = new PrintWriter(new FileOutputStream("/home/agnesko/hello.txt"));
                  out.println(text);
                  out.flush();
                  out.close();
                 }
                 catch(Exception e) {
                  e.printStackTrace();
                 }

Any questions, leave another comment :)

Laminamia :)
0
 
Laminamia063099Commented:
Agnesko,

I believe the above code will work for you if inserted into your servlet.  It uses a PrintWriter to take care of writing the String and it flushes the output buffer to ensure that the string is completely written.  The PrintWriter wraps the FileOutputStream.

Good luck

Laminamia :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Laminamia063099Commented:
Oops, please make the following amendment to the code I posted, agnesko:

PrintWriter out = new PrintWriter(new                
                                           FileWriter("/home/agnesko/hello.txt"));

:)
0
 
agneskoAuthor Commented:
Laminamia,
  Still, I can't create a file and even write things into it. I am not sure if different system have different performance. My servlet run on the top of apache, jsdk1.0, jdk1.1.8, linux.

  Any other suggestion?


0
 
heyhey_Commented:
>> e.printStackTrace();

take a look at the standard error output ?
0
 
agneskoAuthor Commented:
the standard error output shows that
java.io.FileNotFoundException: /home/agnesko/hello.txt
0
 
heyhey_Commented:
do you have such directory ?
/home/agnesko/

if no - you hav to create it first (File.mkdirs())
0
 
snjkrvaCommented:
i have better idea here....

why don't you use Object Serialization instead of writing onto the file...i w m writing down a sample code..

public static void main(String args[]) {
 String text = "Hello World";  
try {
   FileOutputStream out = new FileOutputStream("objfile");
   SerializedObject obj = new SerializedObject(1,'c',text);
   serialzedStream.writeObject(obj);
   serializedStream.flush();
}catch(Exception e) {
   System.out.println("Error during serialazation");
}
0
 
agneskoAuthor Commented:
the standard error output shows that
java.io.FileNotFoundException: /home/agnesko/hello.txt
0
 
agneskoAuthor Commented:
the standard error output shows that
java.io.FileNotFoundException: /home/agnesko/hello.txt
0
 
snjkrvaCommented:
check the file's mode...
chmod 775 hello.txt
0
 
yoavdoCommented:
try the folowing method:
the errors is a vector that hold lines(curently of errors) it write them to a file, u need to create a file like this:

outputFile = new File( "your file full path/ or any other constructors" );

and then call the method:


/**
 * writeFile
 * Purpose : Writting all lines stored in
            a vector to a given file
 * Input   : outputFile name.
 * Output  : none.
**/
public static void writeFile( File outputFile){
      try {
            FileWriter fileWriter = new FileWriter ( outputFile );
            String line = null;
            for(int idx=0;idx<errors.size();idx++){
                line = (String)errors.elementAt(idx);
                line +="\n";                
                fileWriter.write(line);
            }
            fileWriter.close();
            }

      catch(Exception e){            
            e.printStackTrace();
      }

}


hope that will be o.k.
yoav
0
 
agneskoAuthor Commented:
The solution is quite similar to that. The directory containing that file have to set to 777 if the file doesn't exist before.
I think it is not possible to maintain the permission as 700 unless performing some complicated system setting.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now