Reputation: 21
Since Applets run in sandbox mode in browsers, I am using AccessController.doPrivileged to write to a file. It writes to the file when I run it in Eclipse, but doesn't write when I access the applet in browser. What am I missing? Here is the code:
public class HelloWorld extends Applet {
public void paint(Graphics g) {
AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
try {
System.out.println(System.getProperty("user.home"));
String userHome = System.getProperty("user.home");
FileWriter fw = new FileWriter(userHome + File.separator
+ "test" + File.separator + "area.txt");
fw.write("The area is 20m");
fw.flush();
fw.close();
} catch (IOException ioe) {
System.err.println(ioe);
}
return Boolean.TRUE;
}
});
}
}
Upvotes: 2
Views: 4822
Reputation: 168825
AccessController.doPrivileged
does not do what you think1.
But first to the two (practical) ways that an applet can access the local file system.
You might note that I did not list 'adjust policy files/settings' in the list of practical ways. That is because it is not really practical. At least not for anything beyond a closed intranet in which the person deploying them controls the target machines (& can thereby install a policy file to allow the applet trust). But then in that situation, the benefits of an applet are severely eroded in any case.
Upvotes: 4