1.Generate key:
在 command line下输入
keyname 是你自己设的。里叫做叫mysigner 。
keytool -genkey -alias mysigner RSA -keysize 1024 -keystore mystore -validity 3650
其中3650是有效天数,回车后将提示你输入一些信息,按提示输入即可。
2.Export key:
将证书导出到证书文件中,在命令行中输入如下指令
keytool -export -alias mysigner -file mysigner.crt -keystore mylib
要求你输入密码
3.将class打包
jar cvf applet.jar *.class
4.授权jar文件,在命令行中输入如下指令
jarsigner -keystore mylib applet.jar mysigner
5.每次重新编译完class文件后,重复3,4步骤即可。
以下附applet打开本地源代码:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FileAccessApplet extends JApplet {
private JTextField
filename = new JTextField(),
dir = new JTextField();
private JButton open = new JButton("Open");
private JEditorPane ep = new JEditorPane();
private JScrollPane jsp = new JScrollPane();
private File file;
public void init() {
JPanel p = new JPanel();
open.addActionListener(new OpenL());
p.add(open);
Container cp = getContentPane();
jsp.getViewport().add(ep);
cp.add(jsp, BorderLayout.CENTER);
cp.add(p, BorderLayout.SOUTH);
dir.setEditable(false);
ep.setContentType("text/html");
filename.setEditable(false);
p = new JPanel();
p.setLayout(new GridLayout(2, 1));
p.add(filename);
p.add(dir);
cp.add(p, BorderLayout.NORTH);
}
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
c.setFileFilter(new TextFileFilter());
// Demonstrate "Open" dialog:
int rVal = c.showOpenDialog(FileAccessApplet.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
file = c.getSelectedFile();
filename.setText(file.getName());
dir.setText(c.getCurrentDirectory().toString());
try {
System.out.println("Url is " + file.toURL());
ep.setPage(file.toURL());
// ep.repaint();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
if(rVal == JFileChooser.CANCEL_OPTION) {
filename.setText("You pressed cancel");
dir.setText("");
}
}
}
public class TextFileFilter extends
javax.swing.filechooser.FileFilter {
public boolean accept(File f) {
return f.getName().endsWith(".txt")
|| f.isDirectory();
}
public String getDescription() {
return "Text Files (*.txt)";
}
}
}
在 command line下输入
keyname 是你自己设的。里叫做叫mysigner 。
keytool -genkey -alias mysigner RSA -keysize 1024 -keystore mystore -validity 3650
其中3650是有效天数,回车后将提示你输入一些信息,按提示输入即可。
2.Export key:
将证书导出到证书文件中,在命令行中输入如下指令
keytool -export -alias mysigner -file mysigner.crt -keystore mylib
要求你输入密码
3.将class打包
jar cvf applet.jar *.class
4.授权jar文件,在命令行中输入如下指令
jarsigner -keystore mylib applet.jar mysigner
5.每次重新编译完class文件后,重复3,4步骤即可。
以下附applet打开本地源代码:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class FileAccessApplet extends JApplet {
private JTextField
filename = new JTextField(),
dir = new JTextField();
private JButton open = new JButton("Open");
private JEditorPane ep = new JEditorPane();
private JScrollPane jsp = new JScrollPane();
private File file;
public void init() {
JPanel p = new JPanel();
open.addActionListener(new OpenL());
p.add(open);
Container cp = getContentPane();
jsp.getViewport().add(ep);
cp.add(jsp, BorderLayout.CENTER);
cp.add(p, BorderLayout.SOUTH);
dir.setEditable(false);
ep.setContentType("text/html");
filename.setEditable(false);
p = new JPanel();
p.setLayout(new GridLayout(2, 1));
p.add(filename);
p.add(dir);
cp.add(p, BorderLayout.NORTH);
}
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
c.setFileFilter(new TextFileFilter());
// Demonstrate "Open" dialog:
int rVal = c.showOpenDialog(FileAccessApplet.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
file = c.getSelectedFile();
filename.setText(file.getName());
dir.setText(c.getCurrentDirectory().toString());
try {
System.out.println("Url is " + file.toURL());
ep.setPage(file.toURL());
// ep.repaint();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
if(rVal == JFileChooser.CANCEL_OPTION) {
filename.setText("You pressed cancel");
dir.setText("");
}
}
}
public class TextFileFilter extends
javax.swing.filechooser.FileFilter {
public boolean accept(File f) {
return f.getName().endsWith(".txt")
|| f.isDirectory();
}
public String getDescription() {
return "Text Files (*.txt)";
}
}
}