Applet安全权限

时间:2013-01-28 15:09:21

标签: java security applet appletviewer

源代码:

package com.web;

import com.web.Operation;
import java.applet.*;
import java.awt.Graphics;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.io.*;

public class AppletExample extends Applet {

public void init() {
    try {
        getAppletContext().showDocument(new URL("file:///C:/Users/Victor/Desktop/test.txt"), "_blank");
    }
    catch (MalformedURLException ex) {
        System.out.println(ex.getMessage());
    }
}

public void paint( Graphics g ) {
    Operation op = new Operation();
    op.response();
    g.drawString("Go File", 0,100);
}
}

当我使用Appletviewer应用程序运行Applet时,屏幕上会出现下一个错误:

C:\Users\Victor\Desktop\project2\src>appletviewer display.html
Warning: Can't read AppletViewer properties file: C:\Users\Victor\.hotjava\prope
rties Using defaults.
java.security.AccessControlException: access denied ("java.io.FilePermission" "C
:\Users\Victor\Desktop\test.txt" "write")
        at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:366)
        at java.security.AccessController.checkPermission(AccessController.java:
555)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
        at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:203)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
        at java.io.FileWriter.<init>(FileWriter.java:63)
        at com.web.Operation.response(Operation.java:15)
        at com.web.AppletExample.paint(AppletExample.java:25)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
        at sun.awt.RepaintArea.paint(RepaintArea.java:240)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:347)
        at java.awt.Component.dispatchEventImpl(Component.java:4936)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4686)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
        at java.awt.EventQueue.access$000(EventQueue.java:101)
        at java.awt.EventQueue$3.run(EventQueue.java:666)
        at java.awt.EventQueue$3.run(EventQueue.java:664)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:680)
        at java.awt.EventQueue$4.run(EventQueue.java:678)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:211)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:128)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:117)

这是班级操作的作用:

package com.web;

import com.web.AutomatedTelnetClient;
import java.util.*;
import java.io.*;

public class Operation {

public Operation() {
}

public void response() {
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("C://Users/Victor/Desktop/test.txt"));

        AutomatedTelnetClient telnetClient = new AutomatedTelnetClient();
        telnetClient.connect();

        StringBuffer text = telnetClient.sendCommand("display gps");
        telnetClient.disconnect();

        out.write(text.toString());
        out.close();      
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

从applet写入文件似乎是一个权限问题,不是吗?我该如何解决?

1 个答案:

答案 0 :(得分:0)

因为它是您尝试写入的客户端的文件系统。您需要使用类文件创建一个jar,并且必须将其签名为受信任的应用程序。按照post关于如何签名,然后你那个jar来加载applet。