?
Solved

Servlets: writing file to server

Posted on 2000-05-10
14
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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
 
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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

770 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