How to open/read/write a local file from an applet

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

	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 yourkey
Follow 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值