论坛首页 Web前端技术论坛

用数字签名的applet解决客户端文件读写

浏览 3933 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-24  
最近有个java的B/S项目,要求能在客户端读写文件,查了下资料,解决方案一般是写activex和applet,由于项目是跨浏览器的,所以activex不能满足要求,out了,只能用applet,要解决applet在客户端读写的问题,需要给applet数字签名,最后经过几番尝试,终于调试通过,现发出来给大家共享。

首先写applet了
package com.shiningway.applet;

import java.applet.Applet;
import java.io.*;
import java.awt.*;
import java.util.*;
public class ShowMe extends Applet{
	public File f=null;
	public String text="";//请注意此处的public   
	Date dd=null;
	int n=0;


	/**
	 * Called by the browser or applet viewer to inform
	 * this applet that it is being reclaimed and that it should destroy
	 * any resources that it has allocated. The <code>stop</code> method
	 * will always be called before <code>destroy</code>. <p>
	 *
	 * A subclass of <code>Applet</code> should override this method if
	 * it has any operation that it wants to perform before it is
	 * destroyed. For example, an applet with threads would use the
	 * <code>init</code> method to create the threads and the
	 * <code>destroy</code> method to kill them. <p>
	 */
	public void destroy() {
		// Put your code here
	}

	 public void paint(Graphics g ) {
		 if(text!=null && !text.equals(""))
		 {
			 setTxt(text);
		 }
		 //g.drawString(text, 40, 50);
		 text="";
    }
	 
	 public void setText(String st)
	 {
		 
		 text=st;
	 }



	public void init() {
		f=new File("D:\\lala.txt");
		if (!f.exists())
		{
			try
			{
				f.createNewFile();
			}
			catch(Exception ex)
			{
				System.out.println(ex.getMessage());
			}
			
		}
		
	}
	
	public void setTxt(String tt)
	{
		
	
			try
			{
				RandomAccessFile raf = new RandomAccessFile(f, "rw"); 
				raf.seek(raf.length());
				raf.writeBytes("\n"+tt);
				raf.close();
			}
			catch (Exception ex)
			{
				System.out.println(ex.getMessage());
			}
			finally
			{
			
			}
			
			
		
	}

	public void start() {
	
	}

	/**
	 * Called by the browser or applet viewer to inform
	 * this applet that it should stop its execution. It is called when
	 * the Web page that contains this applet has been replaced by
	 * another page, and also just before the applet is to be destroyed. <p>
	 *
	 * A subclass of <code>Applet</code> should override this method if
	 * it has any operation that it wants to perform each time the Web
	 * page containing it is no longer visible. For example, an applet
	 * with animation might want to use the <code>start</code> method to
	 * resume animation, and the <code>stop</code> method to suspend the
	 * animation. <p>
	 */
	public void stop() {
		// Put your code here
	}

}

关于applet的数字签名,网上资料很多,自己可以去查,可以参考一下这个帖子http://www.blogjava.net/beansoft/archive/2008/05/11/199801.html
(其实这个帖子里面已经是包含了我这边的大部分内容了)
showMe.html的文件代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My applet 'ShowMe' starting page</title>
   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  
  </head>
<script>
function setTxt()
{
document.lama.setText(document.getElementById("txtbaby").value);
document.lama.repaint();
}
</script>
  <body>
  <!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<object
    classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
    codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=1,5,0,0"
    WIDTH = "300" HEIGHT = "400" NAME = "lama" >
    <PARAM NAME = CODE VALUE = "com.shiningway.applet.ShowMe.class" >
    <PARAM NAME = ARCHIVE VALUE = "test.jar" >
    <PARAM NAME = NAME VALUE = "lama" >
    <PARAM NAME = MAYSCRIPT VALUE = true >
    <param name = "type" value = "application/x-java-applet;version=1.5">
    <param name = "scriptable" value = "false">

    <comment>
<embed
            type = "application/x-java-applet;version=1.5" \
            CODE = "com.shiningway.applet.ShowMe.class" \
            ARCHIVE = "test.jar" \
            NAME = "lama" \
            WIDTH = "300" \
            HEIGHT = "400" \
            MAYSCRIPT = true
    scriptable = false
    pluginspage = "http://java.sun.com/products/plugin/index.html#download">
    <noembed>
           
            </noembed>
</embed>
    </comment>
</object>

<!--
<APPLET CODE = "com.shiningway.applet.ShowMe.class" ARCHIVE = "test.jar" WIDTH = "300" HEIGHT = "400" NAME = "lama" MAYSCRIPT = true>


</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->

<input type="text" name="txtbaby" id="txtbaby">
<input type="button" onclick="setTxt()" value="添加内容">
  </body>
</html>

这个例子的效果是在showMe.html中的输入框中输入内容,然后点击添加内容按钮,改内容回自动保存在客户端D盘的lala.txt文件中
总结一下,知识点主要是以下几点
  • applet编程
  • applet数字签名
  • java文本文件的读写操作
  • javascript如何与applet通信


疑问:
网页中javascript不能直接调用applet中文件读写的方法setTxt,查看java控制台信息提示无权限,即使用了签名也是如此,后来只有写在paint方法里面,在javascript中用repaint来调用。有没有遇到这个问题的,你们是如何解决的,欢迎来信讨论,email:rautinee@yahoo.com.cn
   发表时间:2010-01-26  
我也借鉴一下。
暂时还没遇到这种需求。
一般我都弄成导入导出的功能,让用户上传和保存有关文件。
0 请登录后投票
   发表时间:2010-05-24  
rautinee 写道
疑问:
网页中javascript不能直接调用applet中文件读写的方法setTxt,查看java控制台信息提示无权限,即使用了签名也是如此,后来只有写在paint方法里面,在javascript中用repaint来调用。有没有遇到这个问题的,你们是如何解决的,欢迎来信讨论,email:rautinee@yahoo.com.cn

//在你的Showme类中加入下面的代码就ok了,
static {
		System.setSecurityManager(null);
	}
0 请登录后投票
   发表时间:2010-05-24  
现在很少有人用applet
一般都是用flash,98%的装机率,无可比拟的优势
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics