日期:2014-05-17  浏览次数:20912 次

js调用applet方法问题。。

public void paint(Graphics g) {
////WriteTxt("abc123");
////WriteEmpty();
g.drawString("1", 2, 20);
File filename = new File("c:\\IvoCall\\IvoCall.txt");
g.drawString("2", 2, 40);
try {
g.drawString("3", 2, 60);
this.IsCreatedFile();
g.drawString("4", 2, 80);
if (!filename.exists()) {
g.drawString("5", 2, 100);
filename.createNewFile();
g.drawString("6", 2, 120);
}

g.drawString("7", 2, 140);
BufferedWriter output = new BufferedWriter(new FileWriter(filename));
g.drawString("8", 2, 160);
output.write("123456");
g.drawString("9", 2, 180);
output.close();
g.drawString("10", 2, 200);
g.drawString("ok", 2, 220);
} catch (IOException e1) {
// TODOe 自动生成 catch 块
e1.printStackTrace();
}
}

这个是我直接在 paint方法中写的内容。 这个applet已经做数字签名了。 所以可以创建文件。 
我在浏览器中直接运行applet是可以创建文件的。 但是我现在想把创建文件的过程封装一个方法,等我想创建的时候在用js调用。可是封装之后再调用的时候就不好使了。 不知道什么原因。 我封装的时候内容和这个是一样的,只不过是复制出来,创建了一个方法把内容复制进去而已。 
封装好的方法是

public void WriteTxt(String strTxt) {
File filename = new File("c:\\IvoCall\\IvoCall.txt");
try {
this.IsCreatedFile();
if (!filename.exists()) {
filename.createNewFile();
}
BufferedWriter output = new BufferedWriter(new FileWriter(filename));
output.write(strTxt);
output.close();
} catch (IOException e1) {
// TODOe 自动生成 catch 块
e1.printStackTrace();
}
}


我html是这样的

<script type="text/jscript">
    function Aa() {
        window.document.AppletTest.WriteTxt("123456d");
    }
</script>
<applet codebase="." 
            code="AppletTest.class" 
            name="AppletTest"
            width="320"
            archive="AppleTest.jar"
            height="240">
    </applet>
    <input type="button" value="确定" onclick="Aa()" />

在applet还有一个测试调用的方法是:

public String TestBack(){
return "abc";
}

如果我在html直接写

  function Aa() {
        alert(window.document.AppletTest.TestBack());
    }
是可以弹出abc的。
我现在不清楚为什么我调用WriteTxt这个方法创建文件的时候,就不好使了。 直接运行是可以创建。
------解决方案--------------------
directoryValid 是我自己封装的 applet 类里的自定义的方法,我把自定义 applet 也给你贴一下吧:
	/**
 * added by Defonds
 * @param filesInfo
 * File Name1<