jsp中applet访问本地文件,怎样才能通过安全策略的限制

我写了一个很简单的applet来读取本地文件,类如下
public class AppletTest extends Applet {
private static final long serialVersionUID = 1L;
private String name;

Font ft = new Font("Courier",Font.BOLD,16);
String str = "jsp嵌入applet,测试成功!";

public void paint(Graphics g){
    g.setFont(ft);
    g.setColor(Color.GREEN);
    g.drawString(str+";->"+name, 10, 50);
}

public void init(){
    super.init();
    try {
        FileInputStream fis = new FileInputStream("c:a.txt");
    InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
    BufferedReader br = new BufferedReader(isr);
    name = br.readLine();
} catch (Exception e) {
    name = e.getMessage();
} 
}

}

jsp如下:




applet 测试





编译过后AppletTest.class生成jar文件并进行了数字签名,但在浏览器中运行该applet时依然报错(已提示用户是否信任该证书)

java.lang.SecurityException: trusted loader attempted to load sandboxed resource from http://localhost:8080/CS/applet/
at com.sun.deploy.security.CPCallbackHandler$ParentCallback.check(Unknown Source)
at com.sun.deploy.security.CPCallbackHandler$ParentCallback.access$1500(Unknown Source)
at com.sun.deploy.security.CPCallbackHandler$ChildElement.checkResource(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.checkResource(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
异常:java.lang.SecurityException: trusted loader attempted to load sandboxed resource from http://localhost:8080/CS/applet/

spinky
spinky 如何解决的?
接近 7 年之前 回复

1个回答

FileInputStream fis = new FileInputStream("c:a.txt");
应是
FileInputStream fis = new FileInputStream("c:\a.txt");

客户端的c盘要有a.txt文件

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问