Strange behaviour of signed applet when accessing a file
Can you advise me please, where is the problem?
I use signed applet to access a file on my HD - I use its method print_file(), see below.
When this method is called from the init() method, it works fine = the content of a file is printed into Java Console. But when applet.print_file() is called from JavaScript, I receive the following error: java.security.AccessControlException: access denied (java.io.FilePermission D:\devel\source\file.xml read).
I use MS IE 6.0 + sp1, JRE 1.4.2_03.
My html page is:
#################
<html>
<head><title>Some title</title> </head>
<body>
<hr />
<applet NAME="TestApplet02"
CODE="test.TestApplet02.class"
CODEBASE="/test"
ARCHIVE="applets/TestApplet02.zip"
WIDTH="0"
HEIGHT="0"
MAYSCRIPT >
</applet>
<script language="JavaScript">
function print_file() {
var retval = TestApplet02.print_file("D:\\devel\\source\\file.xml");
}
</script>
<div align=center>
<h3>Printing a file</h3>
<a href="javascript:void(print_file())"> Print file </a>
</div>
<hr />
</body>
</html>
#################
My TestApplet02.java is:
#################
package test;
import java.applet.Applet;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
public class TestApplet02 extends Applet {
public void init() {
print_file("D:\\devel\\source\\file.xml"); // this works fine, file is printed in Java Console
}
public void start() { }
public void stop() { }
public void destroy() { }
public String print_file(String filename) {
try {
FileInputStream fis=new FileInputStream(filename);
ByteArrayOutputStream tempstream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int i;
while ((i = fis.read(buf, 0, 1024)) != -1) tempstream.write(buf, 0, i);
fis.close();
System.out.println(tempstream.toString());
return "OK";
} catch (Exception e) {
System.out.println("print_file error");
e.printStackTrace();
return("Error");
}
}
}
#################

