Connecting Tech Pros Worldwide Forums | Help | Site Map

Saving file into local disc using Applet

Newbie
 
Join Date: Apr 2009
Posts: 5
#1: May 25 '09
Hello,

can somebody help me with saving file into local disk
using javascript? I made some sample code which
unfortunately won't work :(.

Applet sample file:

Expand|Select|Wrap|Line Numbers
  1. public class IO extends Applet {
  2.  
  3.     public void write(String path, String data) {
  4.         try {
  5.             File file = new File(path);
  6.             DataOutputStream dos = new DataOutputStream(
  7.                 new BufferedOutputStream(
  8.                     new FileOutputStream(file)));
  9.             dos.writeBytes(data);
  10.             dos.close();                    
  11.         }
  12.         catch (SecurityException se) {
  13.             System.err.println(se.getMessage());
  14.         }
  15.         catch (IOException ioe) {
  16.             System.err.println(ioe.getMessage());
  17.         }
  18.     }
  19.  
  20.     public void paint(Graphics g) {
  21.     }
  22.  
  23. }
Html sample file:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <applet archive="IO.jar" code="IO.class" name="io" width="0" height="0"></applet>
  3. <script type="text/javascript">
  4. function saveText() {
  5.     document.applets[0].write("c:\\test.txt", "Hello World!");
  6. }
  7. saveText();
  8. </script>
  9. </html>

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: May 25 '09

re: Saving file into local disc using Applet


What does "doesn't work" mean? Did you get a Permissions exception? If so, there's another thread going on in this forum about the very same subject; you have to make your Applet 'trusted' then by fiddling with a policy file telling the SecurityManager that such an action (writing a file) is an allowed action.

kind regards,

Jos
Newbie
 
Join Date: Apr 2009
Posts: 5
#3: Jul 2 '09

re: Saving file into local disc using Applet


Quote:

Originally Posted by JosAH View Post

What does "doesn't work" mean? Did you get a Permissions exception? If so, there's another thread going on in this forum about the very same subject; you have to make your Applet 'trusted' then by fiddling with a policy file telling the SecurityManager that such an action (writing a file) is an allowed action.

kind regards,

Jos

Yes, I got permission exception. But I don't care about this anymore. Finally I found solution! Watch this video here on youtube: http://www.youtube.com/watch?v=wqpwyAAhKGg
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jul 3 '09

re: Saving file into local disc using Applet


Quote:

Originally Posted by JohnLorac View Post

Yes, I got permission exception. But I don't care about this anymore. Finally I found solution! Watch this video here on youtube: http://www.youtube.com/watch?v=wqpwyAAhKGg

Care to explain how it works? I saw an IO.jar in the text; what code does it contain in its class(es)?

kind regards,

Jos
Newbie
 
Join Date: Apr 2009
Posts: 5
#5: Jul 6 '09

re: Saving file into local disc using Applet


Quote:

Originally Posted by JosAH View Post

Care to explain how it works? I saw an IO.jar in the text; what code does it contain in its class(es)?

kind regards,

Jos

Hello, IO.jar contains signed Java class with five basic methods (write, read, remove, exists and browse) for manipulating with files on local hard disc. There isn't big deal to manage this with signed applet. Everyone can do. There is plenty tutorials how to do this. But, when you want to do the same thing with JavaScript interface then you found there is plenty of problems how to manage cooperation between JavaScript and Java to works together correctly. This solution simply solve this problems ;-).
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Jul 6 '09

re: Saving file into local disc using Applet


Ah, ok, you still need to use a signed applet; I wondered if you found a tricky way to accomplish the same without a trusted applet' thanks for the reply.

kind regards,

Jos
Reply


Similar Java bytes