我正在尝试创建一个将文件保存在确定文件夹中的Applet . 因为我想要根据操作系统的特定文件夹,我使用System.getenv和System.getProperty行 .

我的applet在调试和测试模式下完美运行;但是,我需要发送Applet某些值,我试图通过JavaScript发送,这就是它失败时,给我以下错误:java.security.AccessControlException:access denied(“java.io.FilePermission”) )

到目前为止,我的Applet是自签名和验证的,我正在使用AccessController.doPrivileged来获取系统属性 .

这是我的代码:

import java.io.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.JApplet;

public class SaveSymmetricKey extends JApplet 
{       
    @SuppressWarnings("unchecked")
    public String returnOSUniversalPath()
    {       
        String osName = System.getProperty("os.name").toLowerCase();    
        String universalOSPath = null;
        String osPath = null;
        if (osName.indexOf("windows") > -1) {
            {   
                    osPath = (String)AccessController.doPrivileged(new PrivilegedAction()
                {
                    public Object run()
                    {   
                        try
                        {
                            return System.getenv("APPDATA");
                        }
                        catch(Exception e)
                        {
                            return "No se pudo llevar a cabo el acceso al SO.";
                        }
                    }
                });
                universalOSPath = osPath;
            }
        }
        else if (osName.indexOf("mac") > -1 ) {
            osPath = (String)AccessController.doPrivileged(new PrivilegedAction()
            {
                public Object run()
                {   
                    try
                    {
                        return System.getenv("?");
                    }
                    catch(Exception e)
                {
                    return "No se pudo llevar a cabo el acceso al SO.";
                }
            }
        });
        universalOSPath = osPath;
    }
    else {
        osPath = (String)AccessController.doPrivileged(new PrivilegedAction()
        {
            public Object run()
            {   
                try
                {
                    return System.getProperty("user.home");
                }
                catch(Exception e)
                {
                    return "No se pudo llevar a cabo el acceso al SO.";
                }
            }
        });
    }
    return universalOSPath;
}

//This is the method I call via JavaScript.
public String getKeyFromDotNET(String keyText, String userName) {
    FileOutputStream fos;
    BufferedWriter out;
    String keyPath = returnOSUniversalPath();
    String successReturn = null;

    try
    {   
        File keyFile = new File(keyPath + "\\" + userName + ".pem");
        if (!keyFile.exists()) {
            FileWriter fstream = new FileWriter(keyFile);
            out = new BufferedWriter(fstream);
            out.write(keyText);
            out.close();
            successReturn = "Llave guardada exitosamente.";
        }
        else {
            successReturn = "Llave ya existe.";

        }

        return successReturn;

    }
    catch (IOException e)
    {
        e.printStackTrace();
        successReturn = "No se pudo guardar llave.";

        return successReturn;
    }
}

我究竟做错了什么?提前致谢 . :)