[英]How to get an absolute file path with a 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:fileabs.getFilePath不是函数
我觉得我错过了一些明显的东西。 任何人都可以帮我弄清楚我在这里有什么问题吗?
您需要首先获得对applet DOM元素的引用。 尝试alert(document.getElementById('fileabs').getFilePath());
代码按书面形式工作。 该问题原来是applet的缓存版本,没有我调用的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.