用数字签名的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

相关文章

Idea插件MybatisX失效的问题解决

《Idea插件MybatisX失效的问题解决》:本文主要介绍Idea插件MybatisX失效的问题解决,详细的介绍了4种问题的解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录一、重启idea或者卸载重装MyBATis插件(无需多言)二、检查.XML文件与.Java(该文件后缀Idea可能会隐藏

SpringBoot快速搭建TCP服务端和客户端全过程

《SpringBoot快速搭建TCP服务端和客户端全过程》:本文主要介绍SpringBoot快速搭建TCP服务端和客户端全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录TCPServerTCPClient总结由于工作需要,研究了SpringBoot搭建TCP通信的过程

Nginx 访问 /root/下 403 Forbidden问题解决

《Nginx访问/root/下403Forbidden问题解决》在使用Nginx作为Web服务器时,可能会遇到403Forbidden错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录解决 Nginx 访问 /root/test/1.html 403 Forbidden 问题问题复现Ng

Python的pip在命令行无法使用问题的解决方法

《Python的pip在命令行无法使用问题的解决方法》PIP是通用的Python包管理工具,提供了对Python包的查找、下载、安装、卸载、更新等功能,安装诸如Pygame、Pymysql等Pyt... 目录前言一. pip是什么?二. 为什么无法使用?1. 当我们在命令行输入指令并回车时,一般主要是出现以

Python解决雅努斯问题实例方案详解

《Python解决雅努斯问题实例方案详解》:本文主要介绍Python解决雅努斯问题实例方案,雅努斯问题是指AI生成的3D对象在不同视角下出现不一致性的问题,即从不同角度看物体时,物体的形状会出现不... 目录一、雅努斯简介二、雅努斯问题三、示例代码四、解决方案五、完整解决方案一、雅努斯简介雅努斯(Janu

一文教你如何解决Python开发总是import出错的问题

《一文教你如何解决Python开发总是import出错的问题》经常朋友碰到Python开发的过程中import包报错的问题,所以本文将和大家介绍一下可编辑安装(EditableInstall)模式,可... 目录摘要1. 可编辑安装(Editable Install)模式到底在解决什么问题?2. 原理3.

Python与Java交互出现乱码的问题解决

《Python与Java交互出现乱码的问题解决》在现代软件开发中,跨语言系统的集成已经成为日常工作的一部分,特别是当Python和Java之间进行交互时,编码问题往往会成为导致数据传输错误、乱码以及难... 目录背景:为什么会出现乱码问题产生的场景解决方案:确保统一的UTF-8编码完整代码示例总结在现代软件

idea报错java: 非法字符: ‘\ufeff‘的解决步骤以及说明

《idea报错java:非法字符:‘ufeff‘的解决步骤以及说明》:本文主要介绍idea报错java:非法字符:ufeff的解决步骤以及说明,文章详细解释了为什么在Java中会出现uf... 目录BOM是什么?1. BOM的作用2. 为什么会出现 \ufeff 错误?3. 如何解决 \ufeff 问题?最

Spring三级缓存解决循环依赖的解析过程

《Spring三级缓存解决循环依赖的解析过程》:本文主要介绍Spring三级缓存解决循环依赖的解析过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、循环依赖场景二、三级缓存定义三、解决流程(以ServiceA和ServiceB为例)四、关键机制详解五、设计约

解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException: org.junit.Test问题

《解决tomcat启动时报Junit相关错误java.lang.ClassNotFoundException:org.junit.Test问题》:本文主要介绍解决tomcat启动时报Junit相... 目录tomcat启动时报Junit相关错误Java.lang.ClassNotFoundException