Java与Python之间使用jython工具类实现数据交互
最近有个功能需要java与python之间的数据交互,java需要把参数传给python,然后python计算的结果返回给java.于是就写了一个工具类.
首先,maven需要加载jython的依赖.工具类代码如下:
importjava.util.List; importjava.util.Map; importjava.util.Properties; importorg.apache.poi.ss.formula.functions.T; importorg.python.core.PyFunction; importorg.python.core.PyInteger; importorg.python.core.PyObject; importorg.python.core.PyString; importorg.python.util.PythonInterpreter; /** *@ClassName:JythonUtils *@Description:TODO(jython工具类) *@author:zy *@date: * *@Copyright:2018Inc.Allrightsreserved. *注意: */ publicclassJythonUtils{ /** *@Title:jythonInit *@Description:TODO(初始化jython) *@param:@return *@return:PythonInterpreter *@throws */ publicstaticPythonInterpreterjythonInit(){ //初始化site配置 Propertiesprops=newProperties(); props.put("python.home","");//pythonLib或jythonLib,根据系统中该文件目录路径 props.put("python.console.encoding","UTF-8"); props.put("python.security.respectJavaAccessibility","false"); props.put("python.import.site","false"); Propertiespreprops=System.getProperties(); PythonInterpreter.initialize(preprops,props,newString[0]); //创建PythonInterpreter对象 PythonInterpreterinterp=newPythonInterpreter(); returninterp; } /** *@Title:loadPythonFile *@Description:TODO(加载python源码文件,) *@param:@paraminterp *@param:@paramfilePath,比如:F:\\jpython_jar\\jpythonTest\\pythonTest.py或/testpython/test.py *@param:@return *@return:PythonInterpreter *@throws */ publicstaticPythonInterpreterloadPythonFile(PythonInterpreterinterp,StringfilePath){ interp.execfile(filePath); returninterp; } /** *@Title:loadPythonFunc *@Description:TODO(加载python源码文件中的某个方法) *@param:@paraminterp *@param:@paramfunctionName *@param:@return *@return:PyFunction *@throws */ publicstaticPyFunctionloadPythonFunc(PythonInterpreterinterp,StringfunctionName){ //加载方法 PyFunctionfunc=(PyFunction)interp.get(functionName,PyFunction.class); returnfunc; } /** *@Title:execFunc *@Description:TODO(执行无参方法,返回PyObject) *@param:@paramfunc *@return:PyObject *@throws */ publicstaticPyObjectexecFunc(PyFunctionfunc){ PyObjectpyobj=func.__call__(); returnpyobj; } /** *@Title:execFuncToString *@Description:TODO(执行无参方法,返回一个字符串) *@param:@paramfunc *@param:@return *@return:String *@throws */ publicstaticStringexecFuncToString(PyFunctionfunc){ PyObjectpyobj=execFunc(func); return(String)pyobj.__tojava__(String.class); } /** *@Title:execFuncToString *@Description:TODO(执行有参方法,返回一个字符串) *@param:@paramfunc *@param:@paramparamName,参数名 *@param:@return *@return:String *@throws */ publicstaticStringexecFuncToString2(PyFunctionfunc,StringparamName){ PyObjectpyobj=func.__call__(newPyString(paramName)); return(String)pyobj.__tojava__(String.class); } /** *@Title:execFuncToInteger *@Description:TODO(执行无参方法,返回一个Integer) *@param:@paramfunc *@param:@return *@return:Integer *@throws */ publicIntegerexecFuncToInteger(PyFunctionfunc){ PyObjectpyobj=execFunc(func); return(Integer)pyobj.__tojava__(Integer.class); } /** *@Title:execFuncToList *@Description:TODO(执行无参方法,返回一个List) *@param:@paramfunc *@param:@return *@return:List*@throws */ publicList execFuncToList(PyFunctionfunc){ PyObjectpyobj=execFunc(func); return(List )pyobj.__tojava__(List.class); } /** *@Title:execFuncToMap *@Description:TODO(执行无参方法,返回一个Map ) *@param:@paramfunc *@param:@return *@return:Map *@throws */ publicMap execFuncToMap(PyFunctionfunc){ PyObjectpyobj=execFunc(func); return(Map )pyobj.__tojava__(Map.class); } publicvoidexecFuncToByParamsList(PyFunctionfunc,List paramsList){ } publicstaticvoidmain(String[]args){ PythonInterpreterinterp=jythonInit(); //文件名 StringfilePath="F:\\jpython_jar\\jpythonTest\\pythonTest.py"; interp=loadPythonFile(interp,filePath); //函数名 StringfunctionName="count"; PyFunctionfunc=loadPythonFunc(interp,functionName); //执行无参方法,返回PyObject PyObjectpyobj=execFunc(func); //执行无参方法,返回String StringresultStr=execFuncToString(func); //执行有参方法,返回String StringparamName="name"; StringresultStr2=execFuncToString2(func,paramName); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接