用policytool.exe给applet受予权限

解决方案 »

  1.   

    applet对本地文件系统的存取需要特殊授权否则出错
      

  2.   

    以下是代碼。
    你看這樣的程式需要這麼做嗎?而且我也不能確定該給它加什麼規則呀?
    ---------------------------------------------------------------------------------
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;
    import com.borland.dbswing.*;
    import java.io.*;
    import java.net.*;public class testApplet extends Applet {
      private boolean isStandalone = false;
      private BorderLayout borderLayout1 = new BorderLayout();
      private JTabbedPane jTabbedPane1 = new JTabbedPane();
      private JPanel jPanel1 = new JPanel();
      private BorderLayout borderLayout2 = new BorderLayout();
      private JButton jButton1 = new JButton();
      private JPanel jPanel2 = new JPanel();
      private JTree jTree1 = new JTree();
      private JPanel jPanel3 = new JPanel();
      private JColorChooser jColorChooser1 = new JColorChooser();
      private JPanel jPanel4 = new JPanel();
      private JFileChooser jFileChooser1 = new JFileChooser();
      private JPanel jPanel5 = new JPanel();
      private JPanel jPanel6 = new JPanel();
      private JMenuBar jMenuBar1 = new JMenuBar();
      private JMenu jMenu1 = new JMenu();
      private JMenuItem jMenuItem1 = new JMenuItem();
      private JMenuItem jMenuItem2 = new JMenuItem();
      private JMenuItem jMenuItem3 = new JMenuItem();
      private JMenuItem jMenuItem4 = new JMenuItem();
      private JMenu jMenu2 = new JMenu();
      private JMenuItem jMenuItem5 = new JMenuItem();
      private JMenu jMenu3 = new JMenu();
      private GridLayout gridLayout1 = new GridLayout();
      private BorderLayout borderLayout3 = new BorderLayout();
      private JPanel jPanel7 = new JPanel();
      private JPanel jPanel8 = new JPanel();
      private JPanel jPanel9 = new JPanel();
      private JButton jButton2 = new JButton();
      private JButton jButton3 = new JButton();
      private JPanel jPanel10 = new JPanel();
      private ButtonGroup g1 = new ButtonGroup();
      private JRadioButton jRadioButton1 = new JRadioButton();
      private JRadioButton jRadioButton2 = new JRadioButton();
      private GridLayout gridLayout2 = new GridLayout();
      private VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
      private JRadioButton jRadioButton3 = new JRadioButton();
      private JPanel jPanel11 = new JPanel();
      private JPanel jPanel12 = new JPanel();
      private JTextField jTextField1 = new JTextField();
      private JLabel jLabel1 = new JLabel();
      private JToggleButton jToggleButton1 = new JToggleButton();
      private JTextArea jTextArea1 = new JTextArea();
      private BorderLayout borderLayout4 = new BorderLayout();
      private BorderLayout borderLayout5 = new BorderLayout();  private String docPath = "";
      //Get a parameter value
      public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
          (getParameter(key) != null ? getParameter(key) : def);
      }  //Construct the applet
      public testApplet() {
      }
      //Initialize the applet
      public void init() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      //Component initialization
      private void jbInit() throws Exception {
        this.docPath = getParameter(docPath,"HelloWorldExample.java");
        this.setBackground(UIManager.getColor("Menu.background"));
        this.setLayout(borderLayout1);
        jTabbedPane1.setBackground(Color.orange);
        jPanel1.setLayout(borderLayout2);
        jButton1.setText("jButton1");
        jPanel1.setBackground(SystemColor.activeCaptionBorder);
        jTree1.setBorder(BorderFactory.createLoweredBevelBorder());
        jMenu1.setText("File");
        jMenuItem1.setText("Open");
        jMenuItem2.setText("&New");
        jMenuItem3.setText("-");
        jMenuItem4.setText("!Close");
        jMenu2.setText("Edit");
        jMenuItem5.setText("Null");
        jMenu3.setText("Help");
        jPanel6.setLayout(borderLayout3);
        jPanel6.setToolTipText("");
        jButton2.setText("上一頁");
        jButton3.setText("下一頁");
        jRadioButton1.setBackground(SystemColor.activeCaptionBorder);
        jRadioButton1.setText("jRadioButton1");
        jRadioButton2.setText("jRadioButton2");
        jPanel10.setLayout(verticalFlowLayout1);
        jRadioButton3.setText("jRadioButton3");
        jLabel1.setText("URL");
        jToggleButton1.setText("Get");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jToggleButton1_actionPerformed(e);
          }
        });
      

  3.   

    ---太長所以分開貼的---------------------------------------------------
        jTextArea1.setBackground(new Color(243, 243, 229));
        jPanel5.setLayout(borderLayout4);
        jPanel5.setBackground(SystemColor.desktop);
        jPanel12.setBackground(SystemColor.desktop);
        jPanel12.setLayout(borderLayout5);
        jPanel11.setBackground(new Color(189, 208, 200));
        this.add(jTabbedPane1, BorderLayout.CENTER);
        jTabbedPane1.add(jPanel1,   "Button");
        jPanel1.add(jButton1, BorderLayout.WEST);
        jTabbedPane1.add(jPanel2,   "JTree");
        jTabbedPane1.add(jPanel3,   "null");
        jPanel3.add(jColorChooser1, null);
        jTabbedPane1.add(jPanel4, "jPanel4");
        jPanel4.add(jFileChooser1, null);
        jTabbedPane1.add(jPanel5, "jPanel5");
        jPanel5.add(jPanel12,  BorderLayout.CENTER);
        jPanel12.add(jTextArea1,  BorderLayout.CENTER);
        jPanel5.add(jPanel11, BorderLayout.NORTH);
        jPanel11.add(jLabel1, null);
        jPanel11.add(jTextField1, null);
        jTextField1.setText(""+this.docPath.toString()+"");
        jPanel11.add(jToggleButton1, null);
        jTabbedPane1.add(jPanel6,  "Layout");
        jPanel6.add(jPanel7,  BorderLayout.NORTH);
        jPanel7.add(jPanel9, "jPanel9");
        jPanel9.add(jButton2, null);
        jPanel9.add(jButton3, null);
        jPanel6.add(jPanel8,  BorderLayout.CENTER);
        jPanel8.add(jPanel10,  "jPanel10");
        jPanel7.setLayout(new CardLayout());
        jPanel8.setLayout(new CardLayout());
        jPanel2.add(jTree1, null);
        jMenuBar1.add(jMenu1);
        jMenuBar1.add(jMenu2);
        jMenuBar1.add(jMenu3);
        jMenu1.add(jMenuItem1);
        jMenu1.add(jMenuItem2);
        jMenu1.add(jMenuItem3);
        jMenu1.add(jMenuItem4);
        jMenu2.add(jMenuItem5);
        jPanel10.add(jRadioButton1, null);
        jPanel10.add(jRadioButton2, null);
        jPanel10.add(jRadioButton3, null);
        g1.add(jRadioButton1);
        g1.add(jRadioButton2);
        g1.add(jRadioButton3);
      }
      //Get Applet information
      public String getAppletInfo() {
        return "Applet Information";
      }
      //Get parameter info
      public String[][] getParameterInfo() {
        return null;
      }  void jButton3_actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null,"hell0","hello",1);
      }  void getDocument(){
      try{
        this.docPath = this.docPath==null?"":this.docPath;
        this.docPath = (this.docPath.startsWith("http://") || this.docPath.startsWith("ftp://"))?this.docPath:getCodeBase()+"/"+this.docPath;
        //URL url = new URL(this.docPath);
        URI uri = new URI(this.docPath);
        JOptionPane.showMessageDialog(null,uri.toString(),"URI",1);
        File file = new File(uri);
        FileInputStream fio = new FileInputStream(file);
        byte[] docStr = new byte[fio.available()];
        JOptionPane.showMessageDialog(null,String.valueOf(fio.available()),"file length",1);
        fio.read(new byte[fio.available()]);
        jTextArea1.setText(fio.toString());
        fio = null;
      }catch(Exception ex){
        System.out.println("Error during getDocument!" + ex);
        JOptionPane.showMessageDialog(null,"Error:"+ex,"Get document error",1);
      }
    }  void jToggleButton1_actionPerformed(ActionEvent e) {
        this.docPath = jTextField1.getText()==null?"HelloWorldExample.java":jTextField1.getText().trim();
        try{
        URI uri1 = new URI(this.docPath.toString());
        JOptionPane.showMessageDialog(null,"uri=","URI",1);
        }catch(Exception ex){
          JOptionPane.showMessageDialog(null,"error"+ex,"action error",1);
        }
        getDocument();
      }}
    ------------------------------------------------------------------------------
    純粹的是測試APPLET布局,隻有getDocument()方法是我想取得文件內容的方法。
    麻煩幫忙看看
    謝謝
      

  4.   


    另外請問APPLET的javax.swing.*包裡的類是否要安裝了java 1.2以上,並且在IE裡是選擇了java Vxx(適用於APPLET)後才可以瀏覽它,是不是?
    我剛才測試了,如果IE裡沒有選擇那一項,就會出現applet couldn't be instantiated的信息,這麼一來,javax.swing.*在APPLET裡豈非相當於沒有用?還是有其它的解決方法?謝謝
      

  5.   

    就是读取文件内容的问题
    File file = new File(uri);
        FileInputStream fio = new FileInputStream(file);
        APPLET不允许读写本地机器上的文件
    IE自带的JRE版本比较低,所以如果不使用高版本的JRE,就无法看到含SWING的APPLET。
      

  6.   

    在你的j2re1.4.0\lib\security目录中找到java。policy文件
    加上这个试试:
    grant codeBase "file:${java.home}/lib/ext/*" {
      permission java.io.FilePermission "<<ALL FILES>>", "read, read, write, delete, execute";
    };