public static String test(String key) throws IOException, ScriptException, NoSuchMethodException {
String result = null;
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
URL url = Resources.getResource("filePath"); //文件路径
File file = new File(url.getPath());
FileReader reader = new FileReader(file); // 执行指定脚本
engine.eval(reader);
if(engine instanceof Invocable) {
Invocable invoke = (Invocable) engine; // 调用merge方法
result = (String) invoke.invokeFunction("function",key); //调用了js文件方法及参数
byte[] bytes = ((ScriptObjectMirror) result).to(byte[].class); //转换其他类型的方式
}
return StringUtils.isNotBlank(result) ? result : "";
}
06-23
540
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-16
1597
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)