aspose file tools*
The moose likes Applets and the fly likes Writing a file from an Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Writing a file from an Applet" Watch "Writing a file from an Applet" New topic
Author

Writing a file from an Applet

koralage
Greenhorn

Joined: Mar 15, 2001
Posts: 3
I am writing a small Applet and I need to be able to write to a file from where the Applet came from. I am using a getDocumentBase() to get access to this path. So far I keep getting security exceptions when I try to write. Please give me a code fragment that allows me to write to a Text file from an Applet.
Thanks
Bo
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Please see this related discussion.
Also, your name is NOT in accordance with javaranch policy.
Pl. visit http://www.javaranch.com/name.jsp and re-register.
Thanks for your cooperation.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
koralage
Greenhorn

Joined: Mar 15, 2001
Posts: 3
How exactly do I change my user name. Also I have looked at the links you gave me and so far I don't have a clear picture. I will do some more looking into, but I might have to ask for help again.
Thanks
Bo
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Bo,
Thank you for adhering to the policy. I appreciate your input in this forum very much. I am glad that you understand that we are just trying to maintain a professional atmosphere.
You might have missed the section in our naming policy page that said:

If you are currently using an account that uses an inappropriate alias, please create a new account that follows the rules. The software we use here is not capable of changing the name on an existing account and we have unlimited account slots.
I want to maintain a professional exchange on JavaRanch so that corporate america won't feel embarassed to find employees spending time here.

Unfortunately, as it says, it is not possible to change the user name. To continue posting here, you must re-register with a new name. Here is the link where you can do that: http://www.javaranch.com/cgi-bin/ubb/Ultimate.cgi?action=agree
I hope that you will complete this process so that we can get on to answering your questions and getting your valuable input on the questions of other ranchers.
Thanks again.
Stephanie
Bo Koralage
Greenhorn

Joined: Mar 16, 2001
Posts: 7
Well about signing or giving permission to applets I have had no luck so far. I went through resources you gave and tried to follow few of the examples from sun. I am not clear about setting properties and signatures. Is there any simple example I can look at. All I need to do is to read and write to the server where the applet came from. Thanks for your help
Bo
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Hi Bo!
It is a little difficult to trouble-shoot without seeing your code. How are you attempting to write this file on the server? One option would be to use sockets. There is a very good and simple example of how to implement sockets here: http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
The section titled "Writing a Client/Server Pair" might work for you here.
The section titled "Using a Server to Work Around Security Restrictions", here: http://java.sun.com/docs/books/tutorial/applet/practical/workaround.html
might also help.
If this is not what you had in mind or if I have misunderstood your goal, please post again so that we can find a better solution.
Thanks.
Stephanie
Bo Koralage
Greenhorn

Joined: Mar 16, 2001
Posts: 7
Well I finally I got the Applet to read and write a file. What I had to do was to write a new security policy and add that new policy to my java.security file in the JRE folder. I also required users to use the java plugin. Now I am in the process of moving my files to a web server. The problem I am running into is reading the file from withing the server. I am still using the original code I was used to read the file.
String fileName = "data/test1.dat";
URL url = new URL(myApplet.getDocumentBase(), fileName);
File datFile = new File(url.getFile());
System.out.println(datFile);
if (datFile.exists()){
BufferedReader in = new BufferedReader(new FileReader(datFile));
String line = in.readLine();
....
}

I put in the println for debugging. On the java console it does show me a valid directory file path. However, I am not getting any data for my app and I don't get any security violations.
I guess the bottom line is can an applet open a file from a root webserver directory just as opening one on the file system. Any help you can give is greatly appreciated.
Thanks
Bo
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Just a wild shot:-
Have you tried
URL url=new URL(...);
URLConnection con=url.openConnection();
con.connect();
InputStream is=new con.getInputStream();
BufferedReader in= new BufferedReader(new InputStreamReader(is));
....

Bo Koralage
Greenhorn

Joined: Mar 16, 2001
Posts: 7
Yes that works for reading a file. But I am still not able to write to a file. Here is my last try.
String fileName = "data/temp.dat";
URL url = new URL(calApplet.getDocumentBase(), fileName);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
out = new PrintWriter(connection.getOutputStream());
out.write("This is a test",0, outString.length());
out.write('\n');
Before this when I had the Applet as a file in the dirctory I was using FileWriter and I didn't have any problems. The FileWriter doesn't seem to work when going from a webserver. I would appreciate any help.
Thanks
Bo
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
No, the above method will retrieve the contents of the resource (file), not modify it.
To write to the file, you'll need to have a program running on the server which will receive the data from the client, and then write to the file using FileWriter etc.
One way is to have a simple java server program (say FileWriterServer), running on some port (say 5000), on the same host as your webserver. This program would listen by opening a ServerSocket. The applet will then open a socket connection with the FileWriterServer. Then through the socket, the applet will send the data. The FileWriterServer, will receive the data through the socket, and using the FileWriter, write to the file.
The FileWriterServer could also be implemented as RMI Server exposing a remote method writeFile(String, fileURL). The RMI Server would implement the writeFile method, using a FileWriter to write the String parameter to the File pointed by the URL. The applet would then need to call writeFile() method, whenever it needs to write to the file.
Another way to do it is through a servlet. Make a servlet, implementing its doPost method, to receive Post data and then using FileWriter, write that data to the file. This servlet can be called through the URLConnection.
Waiting to hear your comments.
Bo Koralage
Greenhorn

Joined: Mar 16, 2001
Posts: 7
All of these suggestions sound good, but they all require other java apps or servers to be wrtten. I would think that applets should be able to write a basic text files. Is this a limitation of Applets?
Basically I don't know why I have to implement a whole other server app to just write a basic text file. Seems like Sun shoud provide some functionality for this. Please tell me your thoughts
Thanks
Bo
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Do you want the applet to write to the local host? Or do you want the applet to write to a file on a remote host from where it was downloaded?
Bo Koralage
Greenhorn

Joined: Mar 16, 2001
Posts: 7
To the remote host where the applet came from. I already wrote a security policy on the local host to give that permission. I am using the Java plugin so all users will use the JRE which was the best way I saw to keep all users consitant. Thanks for taking the time to help
Bo
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
No you can't directly write a file on the remote host whether you are using an applet, or an application. There must be a server/servlet running on the remote host, to write the file on the remote host. And this is true whether you are using Java or C or C++ ...
A program running on one host cannot directly access the file-system/local resources of another host. Remember that even for reading a file from the remote host you are using a server (eg. web server). You can request the web-server to serve up the contents of a (static) resource or to initiate a program (CGI/Servlet) and then serve up the response of that program but a web-server is not designed to directly write to a file. So use the web-server to call a servlet to write to the file. Or have a server (on a different port from the web-server), to write to the file. Programming a server/servlet is almost a trivial task in Java. It can be done in a few lines of code.
Bo Koralage
Greenhorn

Joined: Mar 16, 2001
Posts: 7
Thanks for your response. I will give that a try.
Bo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing a file from an Applet
 
Similar Threads
Writing applet datas to a file
how to resolve OutOfMemoryError
Java Applet
file write
writing to a local file