简体   繁体   English

从Javascript调用Applet方法

[英]Applet method calling from Javascript

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. 我有一个applet可以从特定的文件夹上载一些文件并删除它们,但是当我从javascript代码中调用applet函数时出了点问题,当我从init()调用该函数时,它工作正常。

My applet code : 我的小程序代码:

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);
      ....

and this is my javascript code: 这是我的JavaScript代码:

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

When I called from javascript only log printed: 当我从javascript调用时,仅打印日志:

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

Note that when I call from init method of applet it works fine. 请注意,当我从applet的init方法调用时,它工作正常。

I wrap my code into a PriviligedAction , but goes only one step forward and hang on 我将代码包装到PriviligedAction ,但只向前走了一步,然后继续

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

The interaction of Java and JS complicates security. Java和JS的交互使安全性复杂化。 The JRE cannot trust the JS, so it decides the entire 'chain of operations' that include your code is untrusted. JRE无法信任JS,因此它决定包括您的代码在内的整个“操作链”都是不受信任的。 There is a way to fix it. 有一种解决方法。

The code needs to be wrapped in a PrivilegedAction and called using one of the AccessController methods that doPrivileged(..) . 该代码需要包装在PrivilegedAction并使用doPrivileged(..)AccessController方法之一进行调用。 Look at the top of the AccessController docs. 查看AccessController文档的顶部。 (above the methods) to see example usage. (方法上方)以查看示例用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM