Solved

Servlets: writing file to server

Posted on 2000-05-10
14
262 Views
Last Modified: 2013-11-24
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
Comment
Question by:agnesko
  • 5
  • 3
  • 3
  • +2
14 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2796396
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
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2796444
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
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2796451
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
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 2796464
Oops, please make the following amendment to the code I posted, agnesko:

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

:)
0
 

Author Comment

by:agnesko
ID: 2796634
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 2796946
>> e.printStackTrace();

take a look at the standard error output ?
0
 

Author Comment

by:agnesko
ID: 2796967
the standard error output shows that
java.io.FileNotFoundException: /home/agnesko/hello.txt
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 16

Expert Comment

by:heyhey_
ID: 2797123
do you have such directory ?
/home/agnesko/

if no - you hav to create it first (File.mkdirs())
0
 

Expert Comment

by:snjkrva
ID: 2798357
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
 

Author Comment

by:agnesko
ID: 2798397
the standard error output shows that
java.io.FileNotFoundException: /home/agnesko/hello.txt
0
 

Author Comment

by:agnesko
ID: 2798398
the standard error output shows that
java.io.FileNotFoundException: /home/agnesko/hello.txt
0
 

Accepted Solution

by:
snjkrva earned 50 total points
ID: 2798427
check the file's mode...
chmod 775 hello.txt
0
 

Expert Comment

by:yoavdo
ID: 2798886
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
 

Author Comment

by:agnesko
ID: 2799827
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now