如何从java小程序创建文件? [英] How to create file from java applet?

查看:24
本文介绍了如何从java小程序创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 excel 文件并使用 java 小程序保存到本地文件系统.签署应用程序后,我可以通过直接调用小程序成功创建文件.但是,当我尝试从 javascript 调用该方法时,它失败了,没有任何错误消息.我想知道是否有任何策略控制来防止从 javascript 调用 java 方法?

 <脚本语言=JavaScript">函数 createExcel(){文档.excel.createExcel();document.excel.setMessage("来自js的你好世界");}<身体><input type="button" value="Generate Excel" onclick="createExcel()"/><applet id='applet' name='excel' archive='Office.jar,poi-3.7-20101029.jar' code='com.broadridge.Office.class' width='100' height='100'>;</小程序>

解决方案

使用 JavaScript 调用的受信任小程序中的方法需要包装在 PrivilegedAction 并使用 AccessController.doPrivileged(..) 变体.

沙盒文件系统访问

插件 2 JRE(例如 Oracle 的 1.6.0_10+ JRE)使用 JNLP API 文件服务(特别是 FileSaveService).请参阅文件服务演示.示例.

小程序元素

JS 调用的小程序应该在 HTML 中声明它是 scriptable.至于获取HTML",编写它的最佳方法是使用 Oracle 的 deployJava.js.它不仅会以目前每个浏览器已知的最佳方式编写小程序元素,还会检查 JRE 是否存在(是否安装了 Java?)和最低版本(JRE 是运行此小程序所需的最低版本吗?).

I'm trying to create an excel file and save to local file system using java applet. After sign the application, i can successfully create the file by directly invoking the applet. However, when i try to call the method from javascript, it failed without any error message. I'm wondering if there is any policy control to prevent java method to be called from javascript?

 <html>
<script language="JavaScript">
function createExcel()
{
    document.excel.createExcel();

    document.excel.setMessage("hello world from js");
}
</script>
<body>
<input type="button" value="Generate Excel" onclick="createExcel()" />
<applet id='applet' name='excel' archive='Office.jar,poi-3.7-20101029.jar' code='com.broadridge.Office.class' width='100' height='100'></applet>
</body>
</html>

解决方案

Methods in a trusted applet that are invoked using JavaScript need to be wrapped in a PrivilegedAction and called using one of the AccessController.doPrivileged(..) variants.

Sandboxed file-system access

Plug-In 2 JREs (Oracle's 1.6.0_10+ JRE for example) offer sandboxed access to the local file-system using the JNLP API file services (specifically the FileSaveService). See the file service demo. for an example.

Applet element

An applet called by JS should declare that it is scriptable in the HTML. As for getting that 'HTML', the best way to write it is using Oracle's deployJava.js. It will not only write the applet element the best way that is currently known for each browser, but does JRE presence (is Java installed?) and minimum version (is the JRE the minimum version needed to run this applet?) checking.

这篇关于如何从java小程序创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆