Java调用JS代码

    private static void callJs(String data) throws Exception {
    ScriptEngineManager scriptEngineManager =new ScriptEngineManager();
    ScriptEngine engine = scriptEngineManager.getEngineByName("javascript");
    engine.eval(readJsFile());
    Invocable inv = (Invocable) engine;
    Object res = (Object) inv.invokeFunction("n", new String[] {data});
    System.out.println("解密结果"+res);
  }

  private static String readJsFile() throws Exception{
    StringBuffer script = new StringBuffer();
    File file = new File("E:\\BaiduNetdiskDownload\\imooc\\destm.js");
    FileReader filereader = new FileReader(file);
    BufferedReader bufferreader = new BufferedReader(filereader);
    String tempString = null;
    while ((tempString = bufferreader.readLine()) != null) {
      script.append(tempString).append("\n");
    }
    bufferreader.close();
    filereader.close();
    return script.toString();

  }
Last modification:August 18th, 2019 at 09:50 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment