This page shows a method for accessing/reading/writing local files from a JAVA applet in your browser. Bascially you just need to sign the applet with a certificate and you're ready to go.
<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <script> window.google_render_ad(); </script>
The code: displays a filedialog, opens, reads and displays the file (in the JAVA console):
See below for a full applet
Now we need to make an (inofficial) certificate. It IS a certificate, but the browser-plugin will display a certificate-requester, which says that it is unofficial. We do not need to care about this - the user has to decide if he trusts you or not. Maybe you want to state on your page, that the plugin says it is unofficial and therefore not trustworthy, but the user has to accept it in order to run the applet. The permissions are the same - official or unofficial - that doesn't count.
<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <script> window.google_render_ad(); </script>
OK, here we go:
'keytool' is delivered with Sun's development kit.
Now we make the certificate:
Now we have to sign the applet:
Just make a *.bat file including this:
The batch-file compiles the applet, makes a jar-archive and signs the jar-file.
The HTML-code to display the applet:
Now we are done! The applet is signed and if the user accepts the certificate, the applet is allowed to access local files. If the user doesn't agree, this appears in the console:
java.security.AccessControlException: access denied (java.io.FilePermission yourfilename read) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270) at java.security.AccessController.checkPermission(AccessController.java:401) at java.lang.SecurityManager.checkPermission(SecurityManager.java:542) at java.lang.SecurityManager.checkRead(SecurityManager.java:887)
Doesn't look good. It isn't! The accesscontroller checks if it is allowed to read the file. The certificate was not accepted, therefore the applet isn't allowed.
The full applet source code:
The applet displays a small button "open". Click on it, the file requester shows up and select a small text file i.e. on your harddisk. The content of the text file is displayed in the JAVA console.
<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <script> window.google_render_ad(); </script>
localfile.java
index.html
Compile, pack and sign the applet with:
<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <script> window.google_render_ad(); </script>
The code: displays a filedialog, opens, reads and displays the file (in the JAVA console):
See below for a full applet
int arrlen = 10000; byte[] infile = new byte[arrlen]; Frame parent = new Frame(); FileDialog fd = new FileDialog(parent, "Please choose a file:", FileDialog.LOAD); fd.show(); String selectedItem = fd.getFile(); if (selectedItem == null) { // no file selected } else { File ffile = new File( fd.getDirectory() + File.separator + fd.getFile()); // read the file System.out.println("reading file " + fd.getDirectory() + File.separator + fd.getFile() ); try { FileInputStream fis = new FileInputStream(ffile); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); try { int filelength = dis.read(infile); String filestring = new String(infile, 0, filelength); System.out.println("FILE CONTENT=" + filestring); } catch(IOException iox) { System.out.println("File read error..."); iox.printStackTrace(); } } catch (FileNotFoundException fnf) { System.out.println("File not found..."); fnf.printStackTrace(); } }
Now we need to make an (inofficial) certificate. It IS a certificate, but the browser-plugin will display a certificate-requester, which says that it is unofficial. We do not need to care about this - the user has to decide if he trusts you or not. Maybe you want to state on your page, that the plugin says it is unofficial and therefore not trustworthy, but the user has to accept it in order to run the applet. The permissions are the same - official or unofficial - that doesn't count.
<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <script> window.google_render_ad(); </script>
OK, here we go:
'keytool' is delivered with Sun's development kit.
keytool -genkey -keyalg rsa -alias yourkeyFollow the instructions and type in all needed information.
Now we make the certificate:
keytool -export -alias yourkey -file yourcert.crt
Now we have to sign the applet:
Just make a *.bat file including this:
javac yourapplet.java jar cvf yourapplet.jar yourapplet.class jarsigner yourapplet.jar yourkey
The batch-file compiles the applet, makes a jar-archive and signs the jar-file.
The HTML-code to display the applet:
<applet code="yourapplet.class" archive="yourapplet.jar" width="600" height="500"> </applet>
Now we are done! The applet is signed and if the user accepts the certificate, the applet is allowed to access local files. If the user doesn't agree, this appears in the console:
java.security.AccessControlException: access denied (java.io.FilePermission yourfilename read) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270) at java.security.AccessController.checkPermission(AccessController.java:401) at java.lang.SecurityManager.checkPermission(SecurityManager.java:542) at java.lang.SecurityManager.checkRead(SecurityManager.java:887)
Doesn't look good. It isn't! The accesscontroller checks if it is allowed to read the file. The certificate was not accepted, therefore the applet isn't allowed.
The full applet source code:
The applet displays a small button "open". Click on it, the file requester shows up and select a small text file i.e. on your harddisk. The content of the text file is displayed in the JAVA console.
<script type=text/javascript> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <script> window.google_render_ad(); </script>
localfile.java
import java.applet.*; import java.awt.*; import java.util.*; import java.lang.*; import java.text.*; import java.awt.event.*; import java.io.*; public class localfile extends Applet { public localfile() { Panel p = new Panel(); Font f; String osname = System.getProperty("os.name",""); if (!osname.startsWith("Windows")) { f = new Font("Arial",Font.BOLD,10); } else { f = new Font("Verdana",Font.BOLD,12); } p.setFont(f); p.add(new Button("Open")); p.setBackground(new Color(255, 255, 255)); add("North",p); } public boolean action(Event evt, Object arg) { if (arg.equals("Open")) { System.out.println("OPEN CLICKED"); int arrlen = 10000; byte[] infile = new byte[arrlen]; Frame parent = new Frame(); FileDialog fd = new FileDialog(parent, "Please choose a file:", FileDialog.LOAD); fd.show(); String selectedItem = fd.getFile(); if (selectedItem == null) { // no file selected } else { File ffile = new File( fd.getDirectory() + File.separator + fd.getFile()); // read the file System.out.println("reading file " + fd.getDirectory() + File.separator + fd.getFile() ); try { FileInputStream fis = new FileInputStream(ffile); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); try { int filelength = dis.read(infile); String filestring = new String(infile, 0, filelength); System.out.println("FILE CONTENT=" + filestring); } catch(IOException iox) { System.out.println("File read error..."); iox.printStackTrace(); } } catch (FileNotFoundException fnf) { System.out.println("File not found..."); fnf.printStackTrace(); } } } else return false; return true; } }
index.html
<html> <body bgcolor="#dddddd"> <applet code="localfile.class" archive="localfile.jar" width="100" height="100"> </applet> </body> </html>
Compile, pack and sign the applet with:
javac localfile.java jar cvf localfile.jar localfile.class jarsigner localfile.jar yourkeyname