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

2024-04-23 14:38

本文主要是介绍用数字签名的applet解决客户端文件读写,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近有个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" οnclick="setTxt()" value="添加内容">
</body>
</html>

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

疑问:
网页中javascript不能直接调用applet中文件读写的方法setTxt,查看java控制台信息提示无权限,即使用了签名也是如此,后来只有写在paint方法里面,在javascript中用repaint来调用。有没有遇到这个问题的,你们是如何解决的,欢迎来信讨论,email:rautinee@yahoo.com.cn

这篇关于用数字签名的applet解决客户端文件读写的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/929072

相关文章

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题

《解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题》:本文主要介绍解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4... 目录未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘打开pom.XM

XML重复查询一条Sql语句的解决方法

《XML重复查询一条Sql语句的解决方法》文章分析了XML重复查询与日志失效问题,指出因DTO缺少@Data注解导致日志无法格式化、空指针风险及参数穿透,进而引发性能灾难,解决方案为在Controll... 目录一、核心问题:从SQL重复执行到日志失效二、根因剖析:DTO断裂引发的级联故障三、解决方案:修复

IDEA Maven提示:未解析的依赖项的问题及解决

《IDEAMaven提示:未解析的依赖项的问题及解决》:本文主要介绍IDEAMaven提示:未解析的依赖项的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录IDEA Maven提示:未解析的依编程赖项例如总结IDEA Maven提示:未解析的依赖项例如

MySQL主从复制与读写分离的用法解读

《MySQL主从复制与读写分离的用法解读》:本文主要介绍MySQL主从复制与读写分离的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、主从复制mysql主从复制原理实验案例二、读写分离实验案例安装并配置mycat 软件设置mycat读写分离验证mycat读

Redis分片集群、数据读写规则问题小结

《Redis分片集群、数据读写规则问题小结》本文介绍了Redis分片集群的原理,通过数据分片和哈希槽机制解决单机内存限制与写瓶颈问题,实现分布式存储和高并发处理,但存在通信开销大、维护复杂及对事务支持... 目录一、分片集群解android决的问题二、分片集群图解 分片集群特征如何解决的上述问题?(与哨兵模

解决Entity Framework中自增主键的问题

《解决EntityFramework中自增主键的问题》:本文主要介绍解决EntityFramework中自增主键的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录Entity Framework中自增主键问题解决办法1解决办法2解决办法3总结Entity Fram

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错