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

相关文章

2024.6.24 IDEA中文乱码问题(服务器 控制台 TOMcat)实测已解决

1.问题产生原因: 1.文件编码不一致:如果文件的编码方式与IDEA设置的编码方式不一致,就会产生乱码。确保文件和IDEA使用相同的编码,通常是UTF-8。2.IDEA设置问题:检查IDEA的全局编码设置和项目编码设置是否正确。3.终端或控制台编码问题:如果你在终端或控制台看到乱码,可能是终端的编码设置问题。确保终端使用的是支持你的文件的编码方式。 2.解决方案: 1.File -> S

vue同页面多路由懒加载-及可能存在问题的解决方式

先上图,再解释 图一是多路由页面,图二是路由文件。从图一可以看出每个router-view对应的name都不一样。从图二可以看出层路由对应的组件加载方式要跟图一中的name相对应,并且图二的路由层在跟图一对应的页面中要加上components层,多一个s结尾,里面的的方法名就是图一路由的name值,里面还可以照样用懒加载的方式。 页面上其他的路由在路由文件中也跟图二是一样的写法。 附送可能存在

vue+elementui分页输入框回车与页面中@keyup.enter事件冲突解决

解决这个问题的思路只要判断事件源是哪个就好。el分页的回车触发事件是在按下时,抬起并不会再触发。而keyup.enter事件是在抬起时触发。 so,找不到分页的回车事件那就拿keyup.enter事件搞事情。只要判断这个抬起事件的$event中的锚点样式判断不等于分页特有的样式就可以了 @keyup.enter="allKeyup($event)" //页面上的//js中allKeyup(e

vue+elementui--$message提示框被dialog遮罩层挡住问题解决

最近碰到一个先执行this.$message提示内容,然后接着弹出dialog带遮罩层弹框。那么问题来了,message提示框会默认被dialog遮罩层挡住,现在就是要解决这个问题。 由于都是弹框,问题肯定是出在z-index比重问题。由于用$message方式是写在js中而不是写在html中所以不是很好直接去改样式。 不过好在message组件中提供了customClass 属性,我们可以利用

Pycharm配置conda环境(解决新版本无法识别可执行文件问题)

引言: 很多小伙伴在下载最新版本的pycharm或者更新到最新版本后为项目配置conda环境的时候,发现文件夹目录中无法显示可执行文件(一般为python.exe),以下就是本人遇到该问题后试验和解决该问题的一些方法和思路。 一般遇到该问题的人群有两种,一种是刚入门对pycharm进行conda环境配置的小白(例如我),不熟悉相关环境配置的操作和过程,还有一种是入坑pycharm有段时间的老手

青龙面板之Ninja无法安装无法拉库问题解决

因为之前的Ninja库已经不能用了,甚至新找到的库也不能用了,好尴尬,这里使用线下版本进行安装。 ninja安装新方法,其是方法还是原来的,只不过Ninja的库原作者删了,没法直接git了,但是我找到了源码包,我们可以直接通过宝塔面板拖进去。 源码包地址: https://download.csdn.net/download/u012134073/24813485 备用地址: 链接: h

【zabbix】zabbix客户端配置

1、部署zabbix客户端 #zabbix 5.0 版本采用 golang 语言开发的新版本客户端 agent2 。#zabbix 服务端 zabbix_server 默认使用 10051 端口,客户端 zabbix_agent2 默认使用 10050 端口。systemctl disable --now firewalldsetenforce 0hostnamectl set-host

tomcat端口被占用如何解决

转载:https://www.cnblogs.com/demon09/p/9248445.html

BD错误集锦5——java.nio.file.FileSystemException 客户端没有所需的特权

问题:在运行storm本地模式程序时,java.nio.file.FileSystemException  客户端没有所需的特权   解决方式:以管理员身份运行IDEA即可。

# bash: chkconfig: command not found 解决方法

bash: chkconfig: command not found 解决方法 一、chkconfig 错误描述: 这个错误表明在 Bash 环境下,尝试执行 chkconfig 命令,但是系统找不到这个命令。chkconfig 命令是一个用于管理 Linux 系统中服务的启动和停止的工具,通常它是 initscripts 包的一部分,但在最新的 Linux 发行版中可能已经被 syste