• Post Reply Bookmark Topic Watch Topic
  • New Topic

access to local file system works!!!  RSS feed

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Master Rancher
Posts: 2572
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To simulate a button press, post an ActionEvent to the eventqueue:
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter
https://coderanch.com/t/690611/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!