Load local resource in applet
I want to read XML configuration file of the local directory in the applet and I followed the following steps:
1. generate key:
keytool -genkey -keystore acmesoft.store -alias acmeroot
2. Jar class files:
jar cvf MyApplet.jar *.class
3. sign the jar file
jarsigner -keystore acmesoft.store MyApplet.jar acmeroot
4.
keytool -export -keystore acmesoft.store -alias acmeroot -file acmeroot.cert
keytool -import -keystore certs.store -alias acmeroot -file acmeroot.cert
5.make policy file -- applets.policy:
keystore "file:cert.store", "JKS";
grant signedBy "acmeroot"
{
permission java.io.FilePermission "<<ALL FILES>>", "read";
};
6. make html file -- applet.html
<HTML>
<TITLE>Test</TITLE>
<BODY>
<APPLET CODE = "MainFrameApplet.class" ARCHIVE = "MyApplet.jar" WIDTH = 640 HEIGHT = 480>
</APPLET>
</BODY>
</HTML>
7. Use appletviewer:
appletviewer -J-Djava.security.policy=applets.policy applet.html
However, it still doesn't work. The exception is:
java.security.AccessControlException: access denied (java.util.PropertyPermissio
n user.dir read)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:264)
at java.security.AccessController.checkPermission(AccessController.java:
427)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:12
85)
at java.lang.System.getProperty(System.java:627)
at java.io.Win32FileSystem.getUserPath(Win32FileSystem.java:295)
at java.io.Win32FileSystem.resolve(Win32FileSystem.java:311)
at java.io.File.getAbsolutePath(File.java:473)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:167)
at XMLParser.parseNetwork(XMLParser.java:25)
at MainFrameApplet.setComponents(MainFrameApplet.java:64)
at MainFrameApplet.init(MainFrameApplet.java:15)
at sun.applet.AppletPanel.run(AppletPanel.java:373)
at java.lang.Thread.run(Thread.java:595)
What can I do to achieve reading local file in applet? Thanks a lot
[2306 byte] By [
tmdfana] at [2007-10-1 17:14:16]

Use the object tag in your html page.
Looks like all steps have been done correctly including using the policy in the
appletviewer command
Here is an example that should open in the browser without a problem:
http://forum.java.sun.com/thread.jsp?forum=63&thread=409341
4th post explaining how to set up your own policy with your own keystore
Since your applet does not seem to be using any classes that are not allready part of
the signed applet or rt.jar this is not a stack problem. Assuming XMLParser is a class
within your signed applet.
Signing applets (stack problem):
http://forum.java.sun.com/thread.jsp?forum=63&thread=524815
second post and last post for the java class file
If the input file is not at the client side, I just want the applet to read a fixed file in the server side. How I can do to get rid of the security stuff in the server side?
If the applet needs to read a file that is on the same server as the applet use URL and
URLConnection:
String myXMLFile = openURL((new URL(this.getCodeBase(),"../relativePathToXMLFile/xmlFile.xml")).toURI.toASCIIString())
public void openURL(String urlpath) {
// it is VERRY importaint to read the entire response
// if you want to connect to the same server again
// this is because closing the inputstream does not close the socket
// and response data from a previous request could be mixed up with the current
InputStream is;
byte[] buf = new byte[1024];
URLConnection urlc = null;
try {
URL a = new URL(urlpath);
urlc = a.openConnection();
is = urlc.getInputStream();
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = is.read(buf)) > 0) {
bos.write(buf, 0, len);
}
// close the inputstream
is.close();
} catch (IOException e) {
try {
// now failing to read the inputstream does not mean the server did not send
// any data, here is how you can read that data, this is needed for the same
// reason mentioned above.
((HttpURLConnection) urlc).getResponseCode();
InputStream es = ((HttpURLConnection) urlc).getErrorStream();
int ret = 0;
// read the response body
while ((ret = es.read(buf)) > 0) {
}
// close the errorstream
es.close();
} catch (IOException ex) {
// deal with the exception
}
}
}
Sorry, I just fell a bit confusing about your code.
1. getCodeBase() is the method of what class?
2. If the xml file is under the same directory of the applet class. How can I locate it?
URL url = new URL("file://myXML.xml");
3. After the URL is connected, is that mean I need to read every byte and put it into a File instance that can be parsed by DocumentBuilder.
Thanks a lot
> 1. getCodeBase() is the method of what class?
this.getCodeBase() google would have told you this is a method of the applet of wich
your class is a subclass.
> 2. If the xml file is under the same directory of the applet class. How can I locate it?
>URL url = new URL("file://myXML.xml");
new URL(this.getCodeBase(),"myXML".xml");
> 3. After the URL is connected, is that mean I need to read every byte and put it into a
> File instance that can be parsed by DocumentBuilder.
http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html
http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#newDocumentBuilder()
... These input sources are InputStreams, Files, URLs, and SAX InputSources.
The api doc fails to explain how to do that but that's someting we all need to get used
to I guess. (both pages allso con't contain URL so I would not know how to cursturct
or what method to call to pass the URL)
parse uses URI http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html#toURI()(DocumentBuilderFactory.newDocumentBuilder()).parse(new URL(this.getCodeBase(),"myXML.xml")).toURI();
Thanks a lot. I use the input stream to solve the problem. But the parse method doesn't seem to support the URL parameter.And is there any way to write a file in the server side? Thanx again and again.
Can I do it as follows:
URL url = new URL(this.getCodeBase(), "myXML.xml");
URLConnection urlc = url.openConnection();
OutputStream os = urls.getOutputStream();
...
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(_doc), new StreamResult(os));
And if I want the file name to be a parameter, how can I achieve it?