Applet的java.io.FilePermission中的例外 [英] Applet java.io.FilePermission exception

查看:188
本文介绍了Applet的java.io.FilePermission中的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java小程序的,需要能够访问客户端的PC(特别是一个局域网共享)上的文件系统的内部网站上。该applet具有类似于一个功能:

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

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:


  • applet的工作,我做之前,JS调用Java方法
    (论点在参数列表中,小程序重新加载完全时
    需要的话,文件操作均init()方法,恢复到这个工程,但可怕的做法)

  • 该小程序使用自签名的证书

  • 我把一个JFileChooser在init()方法来确保路径是
    没错,它移到了saveFile()方法和对话框不会
    显示。它不会导致一个JS错误显示像它与做
    在Java中.exists()调用,它抛出一个异常的FilePermission在
    try / catch语句

  • ,因为这部作品在init()和没有了saveFile()我只能假设
    这是prevent的JavaScript本身访问文件系统??

推荐答案

要能够从JavaScript调用您的小程序的功能,你必须使用一个接入控制器。
请参见文档

To be able to call your applet functions from JavaScript, you have to use an access controller. See documentation.

因此​​,尝试:

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
        }
    });
}

它的工作原理,甚至使用自签名的证书,你只会得到安全警告。

It works even using a self-signed certificate, you will simply get the security warning.

永远记住,使特权code部分尽可能小。

Always remember to make the privileged code section as small as possible.

这篇关于Applet的java.io.FilePermission中的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆