2012-09-11 33 views
4

叫我有一個小程序來從特定的文件夾上傳一些文件,並刪除它們,但什麼是錯的,當我打電話的小程序功能從我的javascript代碼,當我調用該函數從init()它工作正常。Applet的方法從Javascript

我的applet代碼:

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代碼:

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

當我從JavaScript僅記錄打印稱爲:

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

需要注意的是,當我打電話從init小應用程序的方法,它工作正常。

我總結我的代碼轉換爲PriviligedAction,但前進只有一步和

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

回答

4

掛了Java的互動和JS複雜的安全性。 JRE不能相信JS,所以它決定了包含你的代碼的整個「操作鏈」是不可信的。有一種方法可以解決它。

該代碼需要打包在PrivilegedAction中,並使用AccessController methods之一doPrivileged(..)進行調用。查看AccessController文檔的頂部。 (在方法之上)查看示例用法。

+0

當我添加PrivilagedAction它去log.log(Level.SEVERE, 「上傳functiond2.5」);向前一步,沒有別的。你能再次幫助我嗎? – MKT

+1

我建議你發佈[SSCCE](http://sscce.org/)。我特別感興趣的是''''所暗示的'catch'中發生了什麼。確保它包括一個'e.printStackTrace();'或類似的,並得到Java控制檯輸出(在這裏複製/粘貼)。 BTW大多數這些日誌消息應該在'Level.INFO' –

+1

是的,這是一個空指針異常,我可以不是我的applet類之前看到的,添加註釋@SuppressWarnings後(「串行」),異常來安慰。謝謝。 – MKT