如何使用Java applet获得绝对文件路径



我试图在web浏览器中获得绝对文件路径。我已经了解到,使用纯HTML和javascript是不可能的,java applet是最好的途径。不幸的是,我对java的了解顶多是初级的。到目前为止,我有以下java代码:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.awt.Color;
/*
   <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" 
           width=150 height=100 
 codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0"> 
     <PARAM NAME="code" value="FileApplet.class">
   </OBJECT>
 */
public class fileabs extends JApplet
{
   private JTextField tfCount;
   final JFileChooser fc = new JFileChooser();
  public void init() {
      setBackground(Color.WHITE);
        JPanel p = new JPanel( new FlowLayout(FlowLayout.CENTER, 15, 15));
        p.add(new JLabel("Select File: "));
        tfCount = new JTextField(50);
        tfCount.setEditable(false);
        p.add(tfCount);
        JButton b2 = new JButton("Browse...");
        p.add(b2);
        b2.addActionListener( new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
               tfCount.setText("dsds");
                int returnVal = fc.showOpenDialog(fileabs.this);
                tfCount.setText(fc.getSelectedFile().getAbsolutePath());
            }
        } );
        // p.add(label);
        add(p);
    }
  public String getFilePath() {
    return tfCount.getText();
  }
}

从我在http://jdk6.java.net/plugin2/liveconnect/#JS_TO_JAVA上读到的,我可以从javascript调用applet方法,所以我想出了这个测试网页:

<html>
    <head>
    </head>
    <body>
        <applet id="fileabs" archive="fileabs.jar" code="fileabs" width="960" height="60"></applet>
        <a href="#;" onclick="test()">Test</a>
    <script>
        test = function() {
            alert(fileabs.getFilePath());
        };
    </script>
    </body>
</html>

然而,在firebug控制台中,我得到:

TypeError:将。getFilePath不是一个函数

我觉得我错过了一些明显的东西。有谁能帮我弄清楚我这里有什么问题吗?

首先需要获得对applet DOM元素的引用。尝试alert(document.getElementById('fileabs').getFilePath());

代码按照编写的方式工作。问题原来是applet的缓存版本没有我正在调用的方法。

最新更新