hotyiyu
2011-10-12 14:32 浏览 192
已采纳

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/

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • 已采纳
    u011606457 _1_1_7_ 2011-10-12 15:40

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

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

    点赞 评论 复制链接分享

相关推荐