参考文章:
http://www.cnblogs.com/charlexu/p/3424963.html
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
// http://www.cnblogs.com/charlexu/p/3424963.html
public class JsInServer
{
public static void main(String[]arg)throws IOException
{
// 得到一个ScriptEngine对象
ScriptEngineManager maneger = new ScriptEngineManager();
ScriptEngine engine = maneger.getEngineByName("JavaScript");
// 读js文件
String jsFile = "F:\\工作\\tmp\\pp_index_dl.js";
FileInputStream fileInputStream = new FileInputStream(new File(jsFile));
Reader scriptReader = new InputStreamReader(fileInputStream, "utf-8");
try
{
engine.eval(scriptReader);
if (engine instanceof Invocable)
{
// 调用JS方法
Invocable invocable = (Invocable)engine;
String result = (String)invocable.invokeFunction("getPass", new Object[]{"123456"});
System.out.println(result);
System.out.println(result.length());
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
scriptReader.close();
}
}
}