0

I have a Java applet for an intranet site which needs to access the file system on the client's PC (specifically a LAN share). This applet has a function similar to:

JSObject win;

public void init() {
    win=(JSObject) JSObject.getWindow(this);
    win.call("appletMsg", new Object[] {"<b>Applet Loaded</b>", "win"});
}

public void saveFile(String filepath, String filename) {
    File theDir = new File(filepath);
    try {
        if (theDir.exists()) { // This throws exception
            win.call("appletMsg", new Object[] {"Directory Exists", "win"});
        }
        else {
            win.call("appletMsg", new Object[] {"Creating Directory...", "msg"});
            if (theDir.mkdir()) {
                win.call("appletMsg", new Object[] {"Directory Created", "win"});
            }
            else win.call("appletMsg", new Object[] {"Directory Creation Failed!", "fail"});
        }
    }
    catch (Exception e) { // This exception is caught
        win.call("appletMsg", new Object[] {"Error Reading Directory!", "fail"});
        win.call("appletMsg", new Object[] {filepath, "fail"});
    }
    // More code for working with files, error happens above this
}

Javascript behind the applet

// call applet method
function save() {
    document.myApplet.saveFile('\\\\LOCATION\\DIR\\DIR\\', 'test.txt');
}

// output responses from applet to div
function appletMsg(response, type) {
    document.getElementById('output').innerHTML+='<br><span class='+type+'>'+response+'</span>';
}

Troubleshooting/Thoughts:

  • the applet worked before I made the JS call the Java method (arguments were in param lists, applet was fully reloaded when needed, file operations were in init() method, reverting to this works but is terrible practice)
  • the applet is signed with a self-cert
  • I put a JFileChooser in the init() method to make sure paths were right, moved it to the saveFile() method and the dialog does not show. It doesn't cause a JS error to show like it did with the .exists() call in Java, it does throw a FilePermission exception in a try/catch
  • since this works in init() and not in saveFile() I can only assume this is to prevent JavaScript itself from accessing the file system??
4

1 回答 1

2

为了能够从 JavaScript 调用您的小程序函数,您必须使用访问控制器。请参阅文档

所以尝试:

public void saveFile(String filepath, String filename) {
    AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            File theDir = new File(filepath);
            try {
                if (theDir.exists()) { // This throws exception
                    win.call("appletMsg", new Object[] { "Directory Exists", "win" });
                } else {
                    win.call("appletMsg", new Object[] { "Creating Directory...", "msg" });
                    if (theDir.mkdir()) {
                        win.call("appletMsg", new Object[] { "Directory Created", "win" });
                    } else
                        win.call("appletMsg", new Object[] { "Directory Creation Failed!", "fail" });
                }
            } catch (Exception e) { // This exception is caught
                win.call("appletMsg", new Object[] { "Error Reading Directory!", "fail" });
                win.call("appletMsg", new Object[] { filepath, "fail" });
            }
            // More code for working with files, error happens above this
        }
    });
}

它甚至可以使用自签名证书,您只会收到安全警告。

永远记住使特权代码部分尽可能小。

于 2013-09-12T06:53:28.220 回答