ftp client applet
ftp client apletaplet 没有签名是无法读取本地文件的,选择文件的那个框也弹不出来,要按如下进行操作:
Applet受限于其“沙箱”安全模型,在正常的情况下是不允许其访问磁盘的任何内容的,然而在我们的实际需求中,难免会有需要其可以访问磁盘的情景。
然而如何让Applet摆脱“沙箱”的限制呢?这需要我们通过数字签名来解决这个问题:
首先确保你的机器上注册了jdk环境变量,然后使用keytool -genkey -keystore supermap.store -alias supermap -validity 365 生成一个supermap.store密钥库文件,别名为supermap。
然后就可以进行我们的数字签名操作了,在进行操作之前将你所需的class文件和相关资源文件一起打包成jar文件,通过jar cvf test.jar package1 package2...将所有需要打包的文件一起打包为test.jar(当然也可以通过 eclipse -> File -> export -> jar ....来打包)。
如果你的程序依赖于其他的类库jar文件,而该类库jar文件将可能会对磁盘文件进行访问,那么同样也需要对该jar文件进行数字签名。
使用jarsigner -keystore supermap.store test.jar supermap对jar文件进行签名,在生成密钥库时指定某密码为123456,在此处生成密钥需要输入该密码123456。签名完成便可以进行html文件的编写了。
在html文件中引用applet代码比较简单如下:<applet codebase="." code="com.supermap.test.Test" archive="test.jar,com.supermap.data.jar ... "(此处可添加多个依赖库) width=1024 height-768></applet>,codebase为一个相对路径,用于指明包所在位置,archive指向所有经过数字签名以及不需要签名的jar包和依赖库。
另附:访问随jar包一起发行的媒体数据如图片,需要注意一些细节。比如为某个JButton设置图片:
JButton button = new JButton();
ImageIcon icon = new ImageIcon(getClass().getResource("picName");
button.setIcon(icon);
此处的picName应为相对其class文件的媒体文件相对路径名,如在class文件所在目录下有一个Resources目录用于存放媒体数据,picName为"Resouces/workspace_open.png"。测试通过。
在进行数字签名的时候,注意生成密钥库的目录应该和jar文件所在目录保持一致,即让keytool能找到密钥库文件supermap.store。
导出证书
keytool -export -keystore supermap.store -alias supermap -file test.cer
然后用下面的方式调用:
public void fileChooseFile(){
AccessController.doPrivileged( new PrivilegedAction() {
public Object run() {
chooseFile();
return null;
}
});
}
public void fireProcessJob(){
AccessController.doPrivileged( new PrivilegedAction() {
public Object run() {
ProcessJob();
return null;
}
});
}
public String local_dir = "c:/";
JSObject getDirWin = null;
public void getDirs() {
getDirWin = JSObject.getWindow(this);
AccessController.doPrivileged( new PrivilegedAction() {
public Object run() {
// 访问本地资源
Properties props=System.getProperties(); //获得系统属性集
String osName = props.getProperty("os.name"); //操作系统名称
String osArch = props.getProperty("os.arch"); //操作系统构架
String osVersion = props.getProperty("os.version"); //操作系统版本
String javahome = props.getProperty("java.home"); //操作系统名称
getDirWin.call("addOneFile", new String[]{ osName +"---" + osVersion + "---" +javahome } ); //调用 javascript 方法
File file = new File(local_dir);
getDirWin.call("addOneFile", new String[]{ "2222222" } ); //调用 javascript 方法
// 可以指定返回文件列表的过滤条件
// 这个例子不返回那些以.开头的文件名
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};
File[] files = file.listFiles( filter );
for (int i = 0; i < files.length; i++) {
getDirWin.call("addOneFile", new String[]{ files.getName() } ); //调用 javascript 方法
}
//String[] children = file.list(filter);
//for (int i = 0; i < children.length; i++) {
////win.call("addOneFile", new String[]{ children } ); //调用 javascript 方法
//}
getDirWin.call("addOneFile", new String[]{ "55555555555" } );
return null;
}
});
}
============222222222222=======================
<1>、生成密匙证书(key certificate),该证书将存储在你的.keystore文件中。Validity指的是密匙的有效期,默认是180,但是这里我们需要一年的时间,所以我们设置为365
keytool -genkey -alias FileFtpApplet -validity 365 -keystore FileFtpApplet.keystore
<2>、用我们的密匙来设计我们的APPLET
jarsigner -keystore FileFtpApplet.keystore FileFtpApplet.jar FileFtpApplet
<3>、导出证书
keytool -export -keystore FileFtpApplet.keystore -alias FileFtpApplet -file FileFtpApplet.cer
页:
[1]