#需求 通过浏览器操作客户端的文件浏览器,打开网站指定文件夹,并对文件进行读写。
#初步调用 ##环境安装
- 浏览器Firefox与Java必须是相同位(目前Firefox Windows只有32位,则Java必须是32位)
- 在Java控制面板-》安全 中添加例外站点,如果为本地,则
file:///I:/
- 在Java控制面板->高级->Java控制台,选择显示控制台,方便查看函数是否调用,并打印输出
##程序结构 如果在html想要直接执行applet.class,则必须把class文件放到包的顶端,才能够执行
##页面,与调用Applet函数内容
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
</head>
<body>
<applet id="TacticApplet" code="TacticApplet.class" width="200" height="200">
Your browser does not support the <code>applet</code> tag.
</applet>
<input class="btn btn-default" type="button" value="Input" onclick="opendir()">
<script type="text/javascript">
function opendir(event)
{
console.log('Page localfile.html function opendir Debug:run');
console.log(window);
var test = TacticApplet;
console.log(test);
test.print();
}
</script>
</body>
##Java代码
import java.applet.Applet;
import java.awt.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* Created by zhanpeng on 16-1-28.
*/
public class TacticApplet extends Applet {
int index = 0;
@Override
public void init() {
System.out.println("Class TacticApplet Method init Debug: run");
Graphics g = this.getGraphics();
paint(g);
}
public void paint( Graphics g )
{
index = index + 1;
g.drawString("Hello Applet!" + index, 45, 45);
}
public void print()
{
System.out.println("Class TacticApplet Method print Debug: run");
}
public void call() throws IOException {
File file = new File("/tmp/applet.txt");
if(!file.exists())
file.createNewFile();
FileWriter fileWriter = new FileWriter(file,true);
fileWriter.write("1");
fileWriter.close();
}
}