从 Javascript 调用 Applet 方法 [英] Applet method calling from Javascript

查看:26
本文介绍了从 Javascript 调用 Applet 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序可以从特定文件夹上传一些文件并删除它们,但是当我从我的 javascript 代码调用小程序函数时出现问题,当我从 init() 调用该函数时工作正常.

I have an applet to upload some files from specific folder and delete them,but something is wrong when I call an applet function from my javascript code, when I call that function from init() it works fine.

我的小程序代码:

public class Uploader extends Applet {
   String serverPath;
   String clientPath;
   private JSObject win;
   @Override
   public void init() {
       serverPath = getParameter("serverPath");
       clientPath = getParameter("clientPath");
       try {
           win = JSObject.getWindow(this);
       } catch (JSException e) {
           log.warning("Can't access JSObject object");
       }
       upload(topic,clientPath);
   }
   public void upload(String topic,String clientPath) {
       log.log(Level.SEVERE, "upload functiond");
       DefaultHttpClient client = new DefaultHttpClient();
       MultipartEntity form = new MultipartEntity();
       log.log(Level.SEVERE, "upload functiond2");
       try {
            File directory = new File(clientPath);
            log.log(Level.SEVERE, "upload functiond2.2");
            File[] files = directory.listFiles();
            log.log(Level.SEVERE, "upload functiond2.5");
            int i = 0;
            for (File file : files) {
                log.log(Level.SEVERE, "upload functiond2.6");
                i++;
                form.addPart("file" + String.valueOf(i), new FileBody(file));
                System.out.println("adding file " + String.valueOf(i) + " " + file);
                log.log(Level.SEVERE, "adding file " + String.valueOf(i) + " " + file);
            }
            log.log(Level.SEVERE, "upload functiond3");
            form.addPart("topic", new StringBody(topic, Charset.forName("UTF-8")));
            form.addPart("action", new StringBody(action, Charset.forName("UTF-8")));
            form.addPart("path", new StringBody(serverPath, Charset.forName("UTF-8")));
            HttpPost post = new HttpPost(serverPath);
      ....

这是我的 javascript 代码:

and this is my javascript code:

document.applet.upload(title,"c:\scan");

当我从 javascript 调用时只打印日志:

When I called from javascript only log printed:

log.log(Level.SEVERE, "upload functiond2.2");

请注意,当我从小程序的 init 方法调用时,它工作正常.

Note that when I call from init method of applet it works fine.

我将我的代码包装成一个 PriviligedAction,但只向前迈进了一步并坚持下去

I wrap my code into a PriviligedAction, but goes only one step forward and hang on

log.log(Level.SEVERE, "upload functiond2.5");

推荐答案

Java 和 JS 的交互使安全性变得复杂.JRE 不能信任 JS,因此它决定包括您的代码的整个操作链"是不受信任的.有办法修复它.

The interaction of Java and JS complicates security. The JRE cannot trust the JS, so it decides the entire 'chain of operations' that include your code is untrusted. There is a way to fix it.

代码需要包裹在PrivilegedAction 并使用 AccessController 方法doPrivileged(..).查看 AccessController 文档的顶部.(在方法上方)查看示例用法.

The code needs to be wrapped in a PrivilegedAction and called using one of the AccessController methods that doPrivileged(..). Look at the top of the AccessController docs. (above the methods) to see example usage.

这篇关于从 Javascript 调用 Applet 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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