1

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

我的小程序在调试和测试模式下完美运行;但是,我需要向 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;
    }
}

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

4

0 回答 0