Solved

Servlets: writing file to server

Posted on 2000-05-10
14
263 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
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.

914 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

16 Experts available now in Live!

Get 1:1 Help Now