先把程序贴出来~ import java.io.*;
import java.net.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;public class Pc extends Applet implements ActionListener
{ TextField text;
Button buttonEnter;
public void init()
{ text = new TextField("0",10);
add(text);
buttonEnter = new Button("确定");
add(buttonEnter);
buttonEnter.addActionListener(this);
}
public void paint(Graphics g)
{
g.drawString("请输入正确的密码",5,80);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == buttonEnter)
{
String  psw = new String("");
String  inp = new String("");
int psw_len,len=0,l,temp;
byte pp[] = new byte[100];
try{
File f = new File("psw"); // psw为密码文件名
FileInputStream readfile = new FileInputStream(f);
psw_len = readfile.read(); // 读取密码文件的第一字节(密码位数)
// 此密码位数其实无用,掩人耳目而已

while( (l = readfile.read(pp,1,psw_len + 1)) != -1)
{
len = l; // 读取密码按字节 并计数
}
for (l = 1; l <= len; l++) // 密码转换
{
temp = (int)pp[l];
temp = temp + 3;
if (temp > 255) temp = temp - 255;
pp[l] = (byte)temp;
}
psw = new String (pp,1,len); // 创建密码串
System.out.println(psw); // 密码测试输出
text.setText(psw); // 测试输出(当applet在浏览器中运行,不能读出密码!!!!!!!!!!!!!!)
readfile.close(); // 关闭
}
catch (IOException exp_file){
System.out.println("File read error:"+exp_file);
text.setText(exp_file);
}

inp = text.getText();
System.out.println(inp);
System.out.println(inp.length());
System.out.println(psw.length());

// 得到窗口句柄
   Container c = text.getParent(); 
c = c.getParent();

System.out.println(c); // 测试输出

if (psw.equals(inp)) // 密码判断逻辑
{
try{
System.out.println("open the URL ");
getAppletContext().showDocument(new URL("http://www.gtsoft.com.cn/newgt/index.htm"));   
}
catch (MalformedURLException e_url){
JOptionPane.showMessageDialog(c,"不能打此页面","警告",
JOptionPane.WARNING_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(c,"error psw","警告",
JOptionPane.WARNING_MESSAGE);
}
}
}
}

解决方案 »

  1.   

    这个applet 中有读取磁盘文件的内容.文件里放的密码.
    当这个 APPLET 使用 appletviewer 时,一切正常~(当然这里还有个页面跳转没有反映的问题:getAppletContext().showDocument(new URL("http://www.gtsoft.com.cn/newgt/index.htm"));   )但是将此applet 放在浏览器中运行时就会在JAVA控制台中出现文件读取异常提示:
    java.security.AccessControlException: access denied (java.io.FilePermission psw read)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkRead(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at Pc.actionPerformed(Pc.java:34)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)程序排版不好~ 真是不好意思~ 看着麻烦了点~
    谢谢~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      

  2.   

    applet是不能存取本地文件系统的,这是安全设置,
    你可以通过https+数字签名解决这个问题
      

  3.   

    谢谢楼上~
    还有为什么我的这一句不起作用啊~~~~~~~
    getAppletContext().showDocument(new URL("http://www.gtsoft.com.cn/newgt/index.htm"));