my dog learned polymorphism*
The moose likes Applets and the fly likes access to local file system works!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "access to local file system works!!!" Watch "access to local file system works!!!" New topic
Author

access to local file system works!!!

Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163
I am sure the subject line catches a lot of attention
There is this example signed applet which can access the local file system (say displays the file size when a filename is entered in a field and the button is clicked).

The applet has a public function called getFileSize(String filename).

when it is called thru the applet, meaning: typing in a file name and clicking the button (JButton- which has a command and actionListener defined on it and which hence causes the actionPerformedEvent which is listened by the applet as it implements the listener, the getFileSize() is called and I see the size displayed fine.

However, I tried to call the same function using javascript. Since it is public function, when I do a

document.APPLETNAME.getFileSize("FILENAMEFULLPATH") should ideally return me the file size just as it will do when a name is typed in the textfield and button pressed.

But, it doesn't do that. File permission exception occurs and file access is denied.

So, finally my question is, how do I simulate an event? How do I simulate a applet buttonclicked event?



regards,
Brian
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
To simulate a button press, post an ActionEvent to the eventqueue:
Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163
Norm,

This is what I did. However when I invoke accessFile() thru javascript, the postEvent method doesn't seem to work or it works but the actionListener code is not reached. In the api for postEvent it says a 1.1 style event is raised, I don't know if that is the issue since my versions are certainly not 1.1. What is the workaround?

[CODE]
// SizeChecker.java
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;

public class SizeChecker extends JApplet implements ActionListener{

private JPanel pane = null;
private JScrollPane scrolling = null;
private JTextPane fileBox = null;
private JTextField tfFilename = null;
private JButton butLoad = null;
private final String LOAD = "load";
private long fileSize=0;

public void init() {
try {
jbInit();
} catch(Exception e) {
e.printStackTrace();
}
}


public String accessFile(String fn) {

String retValue="true";
tfFilename.setText(fn);
try {
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(new ActionEvent(butLoad,ActionEvent.ACTION_PERFORMED,"Button

Pressed"));
} catch (Exception e) {
retValue=e.getMessage();
}
return retValue;
}



public String readFile(String fn) {
String thisLine, ret = "";
try {
FileInputStream fin = new FileInputStream(fn);
BufferedReader myInput = new BufferedReader
(new InputStreamReader(fin));
while ((thisLine = myInput.readLine()) != null) {
ret += thisLine + "\n";
}
} catch (Exception e) {
ret = "Cannot load, exception!";
}
return ret;
}


private void jbInit() throws Exception {
pane = new JPanel();
pane.setBounds(new Rectangle(0, 0, 500, 325));
pane.setLayout(null);
pane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
pane.setBackground(new Color(221, 194, 219));


fileBox = new JTextPane();
fileBox.setText("");
fileBox.setEditable(false);
scrolling = new JScrollPane(fileBox);
scrolling.setBounds(new Rectangle(16, 65, 295, 225));

tfFilename = new JTextField();
tfFilename.setText("");
tfFilename.setBounds(new Rectangle(16, 23, 206, 29));

butLoad = new JButton();
butLoad.setBounds(new Rectangle(231, 23, 80, 30));
butLoad.setText("Load");
butLoad.setActionCommand(LOAD);
butLoad.addActionListener(this);

pane.add(scrolling);
pane.add(tfFilename);
pane.add(butLoad);

setContentPane(pane);
}

public void actionPerformed(ActionEvent e) {
tfFilename.setText("Event Performed");
if (e.getActionCommand().equals(LOAD)) {
fileBox.setText(readFile(tfFilename.getText()));
}
}


}

[/CODE
Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163
I found that a simple. butLoad.doClick() would definitely raise the event. However the applet security system si smart enough to be aware fo that and whenever I do a doClick instead of manual click on the button, I get a file permission Exception.

No clue what could be the workaround
Any ideas??

regards,
Brian
Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163
http://forum.java.sun.com/thread.jspa?threadID=602697&tstart=165

says that it has been disallowed starting from 1.4.2. Meaning, javascript calls to applet methods are considered UNTRUSTED even if the applet itself is signed.

regards,
Brian
 
 
subject: access to local file system works!!!
 
Similar Threads
HTML to Applet
JSF Tomahawk File upload problem calculating image file size
calling java applet function from javascript works in IE,but not in netscape
WebPage to Applet to WebPage to Java
Jscape getfilesize()