特定のフォルダにファイルを保存するアプレットを作成しようとしています。OS に応じて特定のフォルダーが必要なので、System.getenv と System.getProperty の行を使用します。
私のアプレットは、デバッグ モードとテスト モードで完全に動作します。ただし、JavaScript 経由で送信しようとしている特定の値をアプレットに送信する必要があり、これが失敗して次のエラーが表示されます: java.security.AccessControlException: access denied ("java.io.FilePermission" )
これまでのところ、私のアプレットは自己署名および検証済みであり、システム プロパティを取得するために 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;
}
}
私は何を間違っていますか?前もって感謝します。:)